C# Regex And Curly Braces For Repetition -
i'm relatively new regex , still struggling along. repetition source of frustration. i've been asked write regex matching mobile phone numbers of format +447 , string 9 digits. after reading, have come with:
string num = @"+447123456789"; string reg = "^[+447]([0-9]{9})$"; regex filter = new regex(reg);
assuming it'd read, must start +447 ([...] specifying explicit characters match) followed 9 digits 0-9, filter.ismatch(num)
returns false. tried replacing {9} + , returned true, though returned true when should have failed (i.e. num holding value "+4").
given results, think i've misunderstood use of [...] (otherwise simple +4 shouldn't return true) , {...} (i understood mean previous sequence (in case, [0-9]) repeated ... times, in case, 9).
any appreciated, in advance, cprlkleg
you need use ^\+447(\d{9})$
.
you must know +
special charater in regex , hence need backward slash character avoid it. + or . or $
, many others treated special character.
\d
denotes digits. \w
denotes characters + digits + underscore
hope helps.
Comments
Post a Comment