我有一个包含'||'的字符串和'|'我只想在'|'
上标记它例如:
|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
答案 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)