0

How do I extract phone numbers from a long string using regex. Here a phone number is a simple 10 digit number, no formatting eg. 1234567890, 0000000000

Possible duplicate : Extracting phone number from string

But here the accepted answer, just matches numbers (single digit, double digit, etc.. & not necessarily 10 digit phone numbers)

Community
  • 1
  • 1
user96546
  • 57
  • 2
  • 11
  • possible duplicate of [Extracting phone number from string](http://stackoverflow.com/questions/7912779/extracting-phone-number-from-string) – Chris Chambers Dec 13 '13 at 07:22
  • @ChrisChambers : I wrote this already in my question, and I also mentioned why none of the answers there solve the problem – user96546 Dec 13 '13 at 07:27
  • Could any one point out which part of the question is unclear so that I can frame it better. I guess so many answers wouldn't have come, if it were maliciously framed – user96546 Dec 13 '13 at 10:30

5 Answers5

2

To match exactly 10 digits use [0-9]{10}

piet.t
  • 11,718
  • 21
  • 43
  • 52
1

\\d{10}can be used for only 10 digits

But a mobile no does not starts with zero so, do this [1-9]\\d{9}

AJ.
  • 4,526
  • 5
  • 29
  • 41
1
List<String> list= new ArrayList<String>();
Matcher m=Pattern.complie("\\d{10}").matcher(input);
while(m.find())
    list.add(m.group());
Anirudha
  • 32,393
  • 7
  • 68
  • 89
1

Try this regex for match 10 digit number

[0-9]{10}

enter image description here

    String mob = "1234567890";
    if(mob.matches("[0-9]{10}"))
        System.out.println("Valid");
    else
        System.out.println("InValid");
Rakesh KR
  • 6,357
  • 5
  • 40
  • 55
1
     String mobileNo = "[0-9]"; 
    int counter=0; 
    Pattern pattern = Pattern.compile(mobileNo); 
    Matcher matcher = pattern.matcher(The String in which we have to find the number);
    while(matcher.find())
{
System.out.println("STart index is "+matcher.start());
System.out.println("End index is "+matcher.end());
System.out.println(matcher.group);
counter++;
}
if(counter==10)
{
System.out.println("Mobile no. found");
counter=0;
}
Khay
  • 981
  • 1
  • 10
  • 22
  • Whats with theses people who had put the question on hold , I got two things for you guys 1. If you cant understand a question it does not imply that the question is useless and you shld put it on hold, if u wanna know the question just look at the answers too and you'll be clear with question ,i'm sure you must be that much smart , MR PUTTING QUestions on HOLD. 2.And if you think its too easy or something like that ,it does not imply that that you should close the topic , cuz a problem is a problem nothing else , and this community is not only for the people from MIT ,or stanford . – Khay Dec 13 '13 at 08:36