-1

I have got string like str = Adobe Flash Player 11.4.402.287 (11.3 MB), I need to extract only Adobe Flash Player as the output. Pls suggest..

I tried using Regex like :

String str = "Adobe Flash Player 11.4.402.287 (11.3 MB)";
        Pattern p = Pattern.compile("^[a-zA-Z]+([0-9]+).*");
        Matcher m = p.matcher(str);

        if (m.find()) {
            System.out.println(m.group(1));
        }
user1617707
  • 61
  • 1
  • 12

3 Answers3

2

As suggested by @MarkoTopolink, regexp [\\p{L}\\s]+ helped me. thanks.

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
user1617707
  • 61
  • 1
  • 12
1

Try this:

    String str = "Adobe Flash Player 11.4.402.287 (11.3 MB)";
    Pattern p = Pattern.compile("^([a-zA-Z ]+)([0-9]+).*");
    Matcher m = p.matcher(str);

    if (m.find()) {
        System.out.println(m.group(1));
    }

There are two problems in your try:

  1. Grouping is done using (), you did not define a group for the text you actually wanted
  2. You need to add a space to get more than one word.
Gijs Overvliet
  • 2,643
  • 3
  • 28
  • 35
0

You can use regex:

String str = "Adobe Flash Player 11.4.402.287 (11.3 MB)";
String [] strs = str.split("([ |0-9|.|(.*MB)]*) [ |0-9|.|(.*MB)]*");
for (String strng : strs) {
    System.out.println(strng.trim());
}
Kishor Sharma
  • 599
  • 8
  • 15