2

How can I convert to following Js regex into C#

let regex = /^([+]?\d{1,2}[.-\s]?)?(\d{3}[.-]?){2}\d{4}$/;

This patterns matches with following international mobile phone numbers

044668180099,
+49-691-234-5678,
+90-537-325-2345,
90-537-566-7152,

I want to do same matches wirh C#

Please advice

4
  • check here: learn.microsoft.com/en-us/dotnet/api/… there is a solution Commented Mar 25, 2020 at 14:07
  • 1
    Try with this: @"^([+]?\d{1,2}[.\-\s]?)?(\d{3}[.-]?){2}\d{4}$" Commented Mar 25, 2020 at 14:11
  • So just use your regex string pattern as is in C#, keeping in mind that C# does not support regex literals. Commented Mar 25, 2020 at 14:11
  • 1
    also check this answer for spaces: stackoverflow.com/a/16077677/2050306 Commented Mar 25, 2020 at 14:14

1 Answer 1

2

Remove '/' at the start and the end of the string.

Regex.Match(yourline, @"^([+]?\d{1,2}[.\-\s]?)?(\d{3}[.-]?){2}\d{4}$");

See also this page about Regex.Match() from the microsoft docs for more overloads.

Sign up to request clarification or add additional context in comments.

4 Comments

Doesn't work: 'RegExParseException: Invalid Pattern'. @robert's answer in the comments above is correct
Oops, I forgot to escape the '-' character. Updated it.
I'm guessing you didn't actually try it, but it's right now :-)
No, I did not. ;p

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.