我有一个字符串和所需的正则表达式来从字符串中提取的子字符串。正则表达式来提取文本含有管charcters
实施例:this is a|b|c|d whatever e|f|g|h
结果:a|b|c|d, e|f|g|h
但是基于我写,则产生的结果如下Java代码:
Pattern ptyy = Pattern.compile("\\|*.+? ");
Matcher matcher_values = ptyy.matcher("this is a|b|c|d whatever e|f|g|h");
while (matcher_values.find()) {
String line = matcher_values.group(0);
System.out.println(line);
}
结果
this
is
a|b|c|d
whatever
结果不是我所希望的河有什么建议?
尝试['/[^\s|]+(?:\|[^\s|])++g'](https://regex101.com/r/aL9bL4/1) –
您是否只有想要单个字母?还是应该'foo | bar | baz | qux'匹配?而且,它总是正好4项,还是应该匹配?a | b | c? – Bohemian
我也想匹配foo | bar | baz | qux,foo2 | ba2r | baz | qux5以及 –