Reputation: 7768
I have a string
//webcache.googleusercontent.com/search?hl=en&lr=&rlz=1G1GGLQ_IWIL297&q=cache:UadRLVLrpCQJ:http://www.buy.com/prod/pny-geforce-9800-gx2-1gb-512-bit-ddr3-600mhz-pci-e-2-0-dual-dvi-hdmi/207534756.html%2BVCG98GX2XPB%2Bsite%253Abuy.com&ct=clnk
How can I check if it contains a sequence of 9-digits? In this example, it has 207534756
I tried this:
String resultString = Regex.Match(i.ToString(), @"\d+").Value;
if (resultString.Length == 9){}
but it does not work
Upvotes: 0
Views: 342
Reputation: 74375
Try with @"[0-9]{9}"
. It will match exactly 9 digits in a row.
Edit: More correctly is to also ensure that there are no other digits around: @"(^|[^0-9])[0-9]{9}([^0-9]|$)"
Upvotes: 5
Reputation: 3355
'\d{9}'
See http://www.w3schools.com/jsref/jsref_obj_regexp.asp which is just a good reference... there is also RegexBuddy... just Google Regex Reference
Upvotes: 0
Reputation: 498952
Change the regex to match strings of 9 digits:
@"\d{9}"
Also, your current code doesn't work because you are using Regex.Match
which only returns the first match.
You should be using Regex.Matches
and loop over the results.
Upvotes: 1
Reputation: 224886
Just use Regex.IsMatch
and a regular expression with a repetition length specifier:
if(Regex.IsMatch(i.ToString(), @"\d{9}")) {
// ...
}
Upvotes: 4