[英]Regular expression to split between pipes except in brackets
I have the following text line: 我有以下文字行:
|random|[abc|www.abc.org]|1024|
I would like to split these into 3 parts with a regular expression 我想用正则表达式将它们分为3部分
random
[abc|www.abc.org]
1024
Currently the following result is achieved with expression \\| 当前,使用表达式\\ |可获得以下结果
random
[abc
www.abc.org]
1024
My problem is that I cannot exclude the pipe symbol in the middle column surrounded by the brackets []. 我的问题是我不能排除括号[]包围的中间列中的管道符号。
If you have to use split
, you can use the regex 如果必须使用
split
,则可以使用正则表达式
\|(?=$|[^]]+\||\[[^]]+\]\|)
https://regex101.com/r/7OxmiY/1 https://regex101.com/r/7OxmiY/1
It will match a pipe, then lookahead for either: 它将匹配管道,然后向前查找:
$
, the end of the string, so that the final |
$
,字符串的末尾,使最终|
is split on, or 被分割,或
[^]]+\\|
, non- ]
characters until a pipe is reached, ensuring that pipes inside []
s will not be split upon, or ,非
]
字符,直到到达管道为止,确保[]
内的管道不会被分割,或者
\\[[^]]+\\]\\|
- Same as above, except with literal [
and ]
s surrounding the pattern -与上述相同,但模式周围包含文字
[
和]
In Java: 在Java中:
String input = "|random|[abc|www.abc.org]|[test]|1024|";
String[] output = input.split("\\|(?=$|[^]]+\\|)");
You can use follow code: 您可以使用以下代码:
final String regex = "(?<=|)\\[?[\\w.]+\\|?[\\w.]+\\]?(?=|)";
final String string = "|random|[abc|www.abc.org]|[test]|1024|";
final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println("Full match: " + matcher.group(0));
}
Output: 输出:
Full match: random
Full match: [abc|www.abc.org]
Full match: [test]
Full match: 1024
See here at regex101: https://regex101.com/r/Fcb3Wx/1 在regex101上查看此处: https ://regex101.com/r/Fcb3Wx/1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.