Krondorian
Krondorian

Reputation: 626

Matching everything after the first comma in a string

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)

Upvotes: 2

Views: 5097

Answers (6)

Peter Lawrey
Peter Lawrey

Reputation: 533870

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]);

Upvotes: 0

Juvanis
Juvanis

Reputation: 25950

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(",") );

Upvotes: 4

Pshemo
Pshemo

Reputation: 124275

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.

Upvotes: 0

fge
fge

Reputation: 121830

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.

Upvotes: 1

someone
someone

Reputation: 6572

I would do this like

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

    System.out.println(commaSeparatedString.substring(commaSeparatedString.indexOf(",")+1));

Upvotes: 0

sqreept
sqreept

Reputation: 5534

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

String myRegex = "[^,]*,(.*)";

Upvotes: 3

Related Questions