2

I am using java to do a regular expression match. I am using rubular to verify the match and ideone to test my code.

I got a regex from this SO solution , and it matches the group as I want it to in rubular, but my implementation in java is not matching. When it prints 'value', it is printing the value of commaSeparatedString and not matcher.group(1) I want the captured group/output of println to be "v123_gpbpvl-testpv1,v223_gpbpvl-testpv1-iso"

String commaSeparatedString = "Vtest7,v123_gpbpvl-testpv1,v223_gpbpvl-testpv1-iso";
//match everything after first comma
String myRegex = ",(.*)";
Pattern pattern = Pattern.compile(myRegex);
Matcher matcher = pattern.matcher(commaSeparatedString);
 String value = "";
if (matcher.matches())
    value = matcher.group(1);
else
    value = commaSeparatedString;                     
System.out.println(value);

(edit: I left out that commaSeparatedString will not always contain 2 commas. Rather, it will always contain 0 or more commas)

Community
  • 1
  • 1
Krondorian
  • 616
  • 1
  • 9
  • 21

6 Answers6

4

If you don't have to solve it with regex, you can try this:

int size = commaSeparatedString.length();
value = commaSeparatedString.substring(commaSeparatedString.indexOf(",")+1,size);

Namely, the code above returns the substring which starts from the first comma's index.

EDIT:

Sorry, I've omitted the simpler version. Thanks to one of the commentators, you can use this single line as well:

value = commaSeparatedString.substring( commaSeparatedString.indexOf(",") );
Juvanis
  • 25,802
  • 5
  • 69
  • 87
3

The definition of the regex is wrong. It should be:

String myRegex = "[^,]*,(.*)";
sqreept
  • 5,236
  • 3
  • 21
  • 26
  • It need not be. You want to match whatever is after a comma, you don't need to match _all_ the input for that. – fge Jan 10 '13 at 18:01
  • I was actually able to derive the regex that I need from this solution. I ended up using `[^,]*,(.*)` – Krondorian Jan 10 '13 at 18:05
1

You are yet another victim of Java's misguided regex method naming.

.matches() automatically anchors the regex at the beginning and end (which is in total contradiction with the very definition of "regex matching"). The method you are looking for is .find().

However, for such a simple problem, it is better to go with @DelShekasteh's solution.

fge
  • 119,121
  • 33
  • 254
  • 329
  • Note that this also means that your original regex is perfectly fine. No need to change it! – fge Jan 10 '13 at 18:13
0

I would do this like

    String commaSeparatedString = "Vtest7,v123_gpbpvl-testpv1,v223_gpbpvl-testpv1-iso";

    System.out.println(commaSeparatedString.substring(commaSeparatedString.indexOf(",")+1));
someone
  • 6,577
  • 7
  • 37
  • 60
0

Here is another approach with limited split

String[] spl = "Vtest7,v123_gpbpvl-testpv1,v223_gpbpvl-testpv1-iso".split(",", 2);
if (spl.length == 2)
    System.out.println(spl[1]);

Byt IMHO Del's answer is best for your case.

Community
  • 1
  • 1
Pshemo
  • 122,468
  • 25
  • 185
  • 269
0

I would use replaceFirst

String commaSeparatedString = "Vtest7,v123_gpbpvl-testpv1,v223_gpbpvl-testpv1-iso";
System.out.println(commaSeparatedString.replaceFirst(".*?,", ""));

prints

v123_gpbpvl-testpv1,v223_gpbpvl-testpv1-iso

or you could use the shorter but obtuse

System.out.println(commaSeparatedString.split(",", 2)[1]);
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130