4

I am using regular expression to validate the mobile number with the following criteria:

  1. Maximum of 12 numbers.
  2. It should start with Zero.
  3. Will allow only one space (at a non-defined point)
  4. Followed by an optional extension number of up to five digits in length, not including the # sign

My Regular Expression looks like below: (I have tested this with "rubular" tester)

^((0((?=\d* \d*#)[\d ]{,11})(#\d{,5})?)|(0(?:\d{,10})(#\d{,5})?)|(0((?=\d* \d*$)[\d ]{,11})))$

But it's not working in .net regular expression engine.

Can someone tell me, in the above one which part doesn't work with .net regular expression engine? and if I can change anything in the above expression will it work in .Net regular expression engine?

Peter O.
  • 32,158
  • 14
  • 82
  • 96
user1863261
  • 107
  • 1
  • 13
  • Maybe a stupid question, but when you put this in C# you _do_ use a `@"..."` string rather than a `"..."` string, right? – Rawling Dec 07 '12 at 11:20
  • Could you please post the C# code? – Glauco Vinicius Dec 07 '12 at 11:22
  • Please don't post the question [again](http://stackoverflow.com/questions/13717990/regular-expression-for-validating-numbers-with-one-space-and-one-optional-specia) and [again](http://stackoverflow.com/questions/13758870/how-to-validate-a-text-box-with-regular-expression-validator) and [again](http://stackoverflow.com/questions/13720282/regular-expression-to-validate-the-mobile-number-for-the-following-scenario) and [again](http://stackoverflow.com/questions/13717184/regular-expression-for-numbers-with-only-one-space-in-undefined-positions). Edit the (first) post. – Hans Kesting Dec 07 '12 at 13:27

1 Answers1

2

The only problem in your regex is that you are not specifying the starting range anywhere.. so it should be {0,11} or {1,11} not {,11}


You can also use this simplified regex

^(?=(\S*[\s]\S*|\S*)$)0(\s?\d){1,11}\s?(#(\s?\d){1,5})?\s?$

\S matches any character that is not space

(\S*[\s]\S*|\S*)$ matches 0 to many non space character followed by a space followed by 0 to many non space characters till end OR it matches all the non space character till end

(?=) is a positive lookahead which check if particular pattern occurs and if not it will not match!

Anirudha
  • 32,393
  • 7
  • 68
  • 89
  • Hi Thanks for your post.I have tested your above expression but its allowing more then one space.Ex:012345 678 92 .But it should not allow.can you please help me in this? – user1863261 Dec 07 '12 at 11:35
  • Thanks a lot. I have tested above one ,now its accepting only one space But its returning false if there is no space.(Ex:0123456789) But this should not happen.At most only one space or none. – user1863261 Dec 07 '12 at 11:48
  • Awesome... it's working now.Can u please give an explanation for the above one ? – user1863261 Dec 07 '12 at 11:54
  • 2
    `\S` with an upper case "S" also matches any character that is not a space. – Olivier Jacot-Descombes Dec 08 '12 at 20:48
  • @OlivierJacot-Descombes hmm...that makes it more compact..thxxx..edited the ans – Anirudha Dec 09 '12 at 05:08