Reputation: 557
I am really struggling with Regular Expressions and can't seem to extract the number from this string
"id":143331539043251,
I've tried with this ... but I'm getting compilation errors
var regex = new Regex(@""id:"\d+,");
Note that the full string contains other numbers I don't want. I want numbers between id: and the ending ,
Upvotes: 2
Views: 12897
Reputation: 21
If you are open to using LINQ try the following (c#):
string stringVariable = "123cccccbb---556876---==";
var f = (from a in stringVariable.ToCharArray() where Char.IsDigit(a) == true select a);
var number = String.Join("", f);
Upvotes: 2
Reputation: 12632
Try this code:
var match = Regex.Match(input, @"\""id\"":(?<num>\d+)");
var yourNumber = match.Groups["num"].Value;
Then use extracted number yourNumber
as a string or parse it to number type.
Upvotes: 4
Reputation: 498934
If all you need is the digits, just match on that:
[0-9]+
Note that I am not using \d
as that would match on any digit (such as Arabic numerals) in the .NET regex engine.
Update, following comments on the question and on this answer - the following regex will match the pattern and place the matched numbers in a capturing group:
@"""id"":([0-9]+),"
Used as:
Regex.Match(@"""id"":143331539043251,", @"""id"":([0-9]+),").Groups[1].Value
Which returns 143331539043251
.
Upvotes: 3