简体   繁体   English

无法用Java Regex解析String

[英]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: 它是一个路径,()中的数字是边缘的权重,我试图使用java Pattern将其拆分如下:

[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. 但它们都不起作用,它总是将整个字符串作为字段返回。

It looks like you just want String.split("->") ( javadoc ). 看起来你只需要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..

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 所以你可以使用类似str.split("->")如果你更具体地说明你为什么需要这个,也许我们可以理解为什么你试图使用那些复杂的正则表达式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM