0

I already known that that the number can be split easily from String.But i am having problem in regular expression.I am having a String like this,

Call Numbers:
US Toll Free: 1-866-394-4524
UK Toll Free: 08081681755
India Toll Free: 180030121212
Mobile Number: 04412345678
Mobile Number: 08012345678  
Conference Bridge: 12345678

Dial the Dial–In number for your location and when prompted enter the conference code followed by #

I want to display it like this :

18663944524    
08081681755    
180030121212    
04412345678    
08012345678    
123456789

Any answer will be helpful.

Aditya Vikas Devarapalli
  • 3,186
  • 2
  • 36
  • 54
user2841300
  • 353
  • 1
  • 7
  • 23
  • possible duplicate of [Java (Regex?) split string between number/letter combination](http://stackoverflow.com/questions/13374171/java-regex-split-string-between-number-letter-combination) – Bishan Oct 18 '13 at 05:31
  • [What have you tried?](http://www.whathaveyoutried.com/) I mean *besides* asking us. – Andrew Thompson Oct 18 '13 at 05:39

5 Answers5

1
final Pattern myPattern = Pattern.compile("[\\w\\s]+:\\s+([\\d\\-]+)?\\s*");

Keep in mind that ([\\d\\-]+) is a group, and we can grab it. Matching on this should work:

String line = // the current line in the file..

Matcher matcher = myPattern.matcher(line);
if (matcher.matches()) {
  String theNumber = matcher.group(1);
  System.out.println("We matched!!!: " + theNumber);
}
yamafontes
  • 5,552
  • 1
  • 18
  • 18
0

You could try like this:

String phoneStr="US Toll Free: 1-866-394-4524";

Pattern p = Pattern.compile("(\\d+)");
Matcher m = p.matcher(phoneStr);
while(m.find()) {
   System.out.print(phoneStr.group(1));
}

Output:

18663944524

Source: Internet. I used this code for one of my projects. I tested for all of your inputs, it worked fine.

Pradeep Simha
  • 17,683
  • 18
  • 56
  • 107
0

The easy and dirty way would be to replace all non-numerical characters, save from return carriages, with an empty String.

Here's an example:

// your original text
String text = "Call Numbers: \n" + "US Toll Free: 1-866-394-4524\n"
        + "UK Toll Free: 08081681755\n" + "India Toll Free: 180030121212\n"
        + "Mobile Number: 04412345678\n" + "Mobile Number: 08012345678\n\n" +
            "Conference Bridge: 12345678";
// prints the replacement
System.out.println(text.replaceAll("[\\D&&[^\n]]", ""));

Output:

18663944524
08081681755
180030121212
04412345678
08012345678

12345678

Note that there's still whitespace to post-process in this output, namely the 1st return carriage and the double return carriage between the last two lines.

Mena
  • 47,782
  • 11
  • 87
  • 106
0

You can strip out all non-number-containing lines and all non-digits in one line:

str = str.replaceAll("[ :a-zA-Z-]", "").replaceAll("(?m)^$\n", "");

Here's some test code:

String str = "Call Numbers: \n" + "US Toll Free: 1-866-394-4524\n"
    + "UK Toll Free: 08081681755\n" + "India Toll Free: 180030121212\n"
    + "Mobile Number: 04412345678\n" + "Mobile Number: 08012345678\n\n" +
        "Conference Bridge: 12345678";
str = str.replaceAll("[ :a-zA-Z-]", "").replaceAll("(?m)^$\n", "");
System.out.println(":"+str);

Output (there are blank lines):

18663944524
08081681755
180030121212
04412345678
08012345678
12345678
Bohemian
  • 412,405
  • 93
  • 575
  • 722
0

You can use String.replaceAll. Do it as

str.replaceAll("\\D","");

\D stands for anything except digit.

For each string str it returns all digits in str by replacing every other character with empty string.

Naveed S
  • 5,106
  • 4
  • 34
  • 52