0

I want to have phone number in working combination of '0' followed by 10 digit mobile number (or) 0091 folllowed by 10 digit mobile number (or) +91 followed by 10 digit number.

1) 0 followed and +91 folowed numbers are working with following regex i also want 0091 followed number to be worked, my regex is:

"^([0]|(?:[0][0]|\\+)(91))([7-9]{1})([0-9]{9})$";

Could you suggest me working a regex.

V G
  • 18,822
  • 6
  • 51
  • 89

5 Answers5

7

The exact regex you seem to be going for (based on what you've tried so far) is:

^(?:0091|\\+91|0)[7-9][0-9]{9}$
  • Begins with 0, +91 or 0091
  • Followed by a 7-9
  • Followed by exactly 9 numbers
  • No capture groups
  • Must match entire input

Working example on RegExr

As a general tip, to have worked this out yourself I'd advise using a site like RegExr or RegexPal

Set it to multi-line mode (so that ^ and $ match at the end of each line) then add 0091, +91 and 0 into the input box on separate lines - so you have something like this.

Then try to make a regex that matches just that part, in your case you needed something like

^0091|\+91|0$

Note: on RegExr you don't have to escape backslashes (so when you use the regex in java you need to go through escaping them).

OGHaza
  • 4,795
  • 7
  • 23
  • 29
2

Guess this regex would work:

^((0091)|(\+91)|(0))([7-9]{1})([0-9]{9})$
Vignesh
  • 343
  • 2
  • 5
  • 13
1

If you are working on Android, you could use this method: PhoneNumberUtils.isGlobalPhoneNumber("+91.......)

Or try to do something like that if you really want to use regex:

if (match("^0[0-9]{9}") || match("^+91[0-9]{10}") || match("^0091[0-9]{9}"))
Manitoba
  • 8,522
  • 11
  • 60
  • 122
0

Use the following code (test.matches(..) ist the important part):

    String test = "00911234567891";
    System.out.println(test.matches("(0091|0|\\+91)[7-9][0-9]{9}"));
Manuel Manhart
  • 4,819
  • 3
  • 24
  • 28
-1

This regex should work.

You can use ^([0]|\+91)?\d{10} as starts with 0 or +91 and 10 digits after that.

GrIsHu
  • 29,068
  • 10
  • 64
  • 102
  • As far as I can tell, OP doesn't want to match 2 (and in your regex exactly 2) comma separated numbers. – OGHaza Nov 27 '13 at 13:28
  • sorry i already have 0 and +91 working ,i need 0091 to get worked in same regex –  Nov 27 '13 at 13:37