Reputation: 1
I am beginner and have some problems with regexp.
Input text is : something idUser=123654; nick="Tom" something
I need extract value of idUser -> 123456
I try this:
//idUser is already 8 digits number
MatchCollection matchsID = Regex.Matches(pk.html, @"\bidUser=(\w{8})\b");
Text = matchsID[1].Value;
but on output i get idUser=123654, I need only number
The second problem is with nick="Tom", how can I get only text Tom from this expresion.
Upvotes: 0
Views: 97
Reputation: 59461
Use look-around
(?<=idUser=)\d{1,8}(?=(;|$))
To fix length of digits to 6, use (?<=idUser=)\d{6}(?=($|;))
Upvotes: 0
Reputation: 383876
Here's a pattern that should work:
\bidUser=(\d{3,8})\b|\bnick="(\w+)"
Given the input string:
something idUser=123654; nick="Tom" something
This yields 2 matches (as seen on rubular.com):
User=123654
, group 1 captures 123654
nick="Tom"
, group 2 captures Tom
Some variations:
nick
always appears after idUser
, you can match the two at once instead of using alternation as above.{3,8}
repetition to show how to match at least 3 and at most 8 digits.Match.Groups
property
Upvotes: 0
Reputation: 6735
you don't show your output code, where you get the group from your match collection.
Hint: you will need group 1 and not group 0 if you want to have only what is in the parentheses.
Upvotes: 1