please can anyone help me to convert this regular expression pattern from javascript to c#
var phonePattern = /^(([^\.\-\,a-wy-z]([\(]?(\+|[x])?\d+[\)]?)?[\s\.\-\,]?([\(]?\d+[\)]?)?[\s\.\-\,]?(\d+[\s\.\-\,]?)+[^\.\-\,a-z])|((\+|[x])?\d+))$/i
i need to validate phone number i'll take the value from textbox and i'll compare it with regex pattern i need to convert this pattern specifically because This pattern will address almost most of the country phone patterns like UK, US, eurpoe, india etc.
this is my c# code
Regex REphone = new Regex(@" c# pattern here");
if (REphone.IsMatch(TextBox_Phone.Text)) {
// ...
} else {开发者_如何转开发
// ...
}
new Regex(@"^(([^\.\-\,a-wy-z]([\(]?(\+|[x])?\d+[\)]?)?[\s\.\-\,]?([\(]?\d+[\)]?)?[\s\.\-\,]?(\d+[\s\.\-\,]?)+[^\.\-\,a-z])|((\+|[x])?\d+))$",
RegexOptions.IgnoreCase);
The pattern itself is the same, but quoted between @"
, "
instead of /
, /
.
RegexOptions.IgnoreCase
makes the match case-insensitive, which is the equivalent to the i
flag on the end of your JavaScript regex literal.
JavaScript delimits regex using /
and C# doesn't use delimiters (you pass it in as a string so technically you could say it is delimited by double-quotes "
).
Also, in C# strings remember you have to escape backslashes from \
to \\
.
So your regex expression would look like this -
"^(([^\\.\\-\\,a-wy-z]([\\(]?(\\+|[x])?\\d+[\\)]?)?[\\s\\.\\-\\,]?([\\(]?\\d+[\\)]?)?[\\s\\.\\-\\,]?(\\d+[\\s\\.\\-\\,]?)+[^\\.\\-\\,a-z])|((\\+|[x])?\\d+))$"
If you are going to pass it in as a string literal (by using the @
in front of it) then you don't need to escape the backslashes.
精彩评论