当双管道字符存在时,管道上的字符串标记

时间:2014-08-30 05:19:27

标签: regex groovy

我有一个包含'||'的字符串和'|'我只想在'|'

上标记它

例如:

|A||This is some string|B||This is some other String

应该被标记为

[A||This is some string, B||This is some other String]

我尝试使用val.tokenize('\\|'),但这并没有给我想要的结果,即它在'||'上标记我也得到了:

[A, This is some string, B, This is some other String]

我做错了什么?

感谢。

PS:我正在使用Groovy

2 个答案:

答案 0 :(得分:2)

您可以使用lookaround断言。

def s = '|A||This is some string|B||This is some other String'
def m = s.split('(?<!\\|)\\|(?!\\|)')
println m.findAll {it != ''}

虽然做得更短:

def m = s.findAll('[^|]+\\|{2}[^|]+')
assert m == ['A||This is some string', 'B||This is some other String']

输出

[A||This is some string, B||This is some other String]

答案 1 :(得分:1)

您可以使用此正则表达式:

(?<!\|)\|(?!\|)

<强> Working demo

请记住使用双反斜杠:

(?<!\\|)\\|(?!\\|)

enter image description here