user468587
user468587

Reputation: 5031

cannot parse String with Java Regex

I have a string formatted as below:

source1.type1.8371-(12345)->source2.type3.3281-(38270)->source4.type2.903..

It's a path, the number in () is the weight for the edge, I tried to split it using java Pattern as following:

[a-zA-Z.0-9]+-{1}({1}\\d+){1}
[a-zA-Z_]+.[a-zA-Z_]+.(\\d)+-(\\d+)
[a-zA-Z.0-9]+-{1}({1}\\d+){1}-{1}>{1}

hopefully it split the string into fields like

source1.type1.8371-(12345)
source2.type3.3281-(38270)
..

but none of them work, it always return the whole string as the field.

Upvotes: 0

Views: 68

Answers (2)

aaronman
aaronman

Reputation: 18750

It seems to me like you want to split at the ->'s. So you could use something like str.split("->") If you were more specific about why you need this maybe we could understand why you were trying to use those complicated regexes

Upvotes: 1

FDinoff
FDinoff

Reputation: 31429

It looks like you just want String.split("->") (javadoc). This splits on the symbol -> and returns an array containing the parts between ->.

String str = "source1.type1.8371-(12345)->source2.type3.3281-(38270)->source4.type2.903..";
for(String s : str.split("->")){
    System.out.println(s);
}

Output

source1.type1.8371-(12345)
source2.type3.3281-(38270)
source4.type2.903..

Upvotes: 1

Related Questions