cplusplusrat
cplusplusrat

Reputation: 1445

Split string multiple patterns

I have a string of form "Some lengthy string, Pattern1: string1, Pattern2: string2, Pattern3: string3". My objective is to extract string1, string2 and string3 from such a String.

Note that the Strings, Pattern1, Pattern2 and Pattern3 themselves consist of multiple characters. I know the solution if these patterns were single characters. Currently I am forced to do multiple String.split() calls one each for Pattern1, Pattern2 and Pattern3.

Is there a more graceful way of doing this? Or there is no escaping multiple split() calls?

EDIT: I was looking or a java specific solution (Hence Java was in title, but it got edited out). Any way. This is what I tried and it appears to work for me.

String someString = "Some lengthy string Pattern1: string1 Pattern2: string2 Pattern3: string3";
String [] str = someString.split("Pattern1:|Pattern2:|Pattern3:");
System.out.println(str[1]);
System.out.println(str[2]);
System.out.println(str[3]);

Output of this is

string1
string2
string3

Any obvious problems with this?

Upvotes: 2

Views: 723

Answers (3)

Uniphonic
Uniphonic

Reputation: 875

How about a String.replaceAll() instead with capture groups?

str.replaceAll(".*(?:Pattern1: ([^,]*))(?:, Pattern2: ([^,]*))(?:, Pattern3: ([^,]*))", "$1;$2;$3").split(";");

or to make it even shorter, you can remove the patterns and just look for the colons:

str.replaceAll("[^:]*: ([^,]*,?)", "$1").split(",");

Upvotes: 0

Avaneesh
Avaneesh

Reputation: 162

String stringToSearch = "Some lengthy string Pattern1: string1 Pattern2: string2 Pattern3: string3";

String REGEX = "Pattern\\w:\\s[\\w]+";
Pattern p1 = Pattern.compile(REGEX);
Matcher m = p1.matcher(stringToSearch);

Pattern p2 = Pattern.compile(":");

while (m.find())
{
    String[] items = p2.split(m.group());
    if(items.length < 2 )
       break;
    System.out.println(items[1]);   
}

Output:

string1 string2 string3

Upvotes: 0

Bohemian
Bohemian

Reputation: 425198

To get an array of your targets, you first must strip off the leading part, then split on the other patterns:

String targets = input.replaceAll("^.*?Pattern1: ", "").split(", (Pattern2|Pattern3): ");

Upvotes: 1

Related Questions