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.

2
  • no i havent tried anything Commented Nov 27, 2013 at 13:23
  • did you try to play around with a regexp testing site such as: regexpal.com ? Commented Nov 27, 2013 at 13:23

5 Answers 5

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).

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

Comments

2

Guess this regex would work:

^((0091)|(\+91)|(0))([7-9]{1})([0-9]{9})$

Comments

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}"))

1 Comment

thank u for good logic and i gave upvote
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}"));

5 Comments

why / what exactly? I tried it and it returned true for 0091 + number, 0 + number and +91 + number.
ah I see now the problem. You distinguish between mobile and normal number. How is a mobile number identified?
sorry mate its working i upvoted ur answer
@ManuelM. - the new regex is significantly worse than the previous one (which looked about right)
restored the correct version.
-1

This regex should work.

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

2 Comments

As far as I can tell, OP doesn't want to match 2 (and in your regex exactly 2) comma separated numbers.
sorry i already have 0 and +91 working ,i need 0091 to get worked in same regex