使用Java中的正则表达式拆分具有转义序列的字符串 [英] Splitting a string that has escape sequence using regular expression in Java
问题描述
要拆分的字符串
abc:def:ghi\:klm:nop
字符串应根据:拆分。
\是转义字符。所以\:不应被视为令牌。
String should be split based on ":" "\" is escape character. So "\:" should not be treated as token.
split(:)给出
split(":") gives
[abc]
[def]
[ghi\]
[klm]
[nop]
必需的输出是字符串数组
Required output is array of string
[abc]
[def]
[ghi\:klm]
[nop]
如何可以忽略\:
推荐答案
使用后视断言:
split("(?<!\\\\):")
只有在前面没有 \
时才会匹配。使用双重转义 \\\\
是必需的,因为字符串声明需要一个,正则表达式需要一个。
This will only match if there is no preceding \
. Using double escaping \\\\
is required as one is required for the string declaration and one for the regular expression.
但是请注意,在您希望允许令牌以反斜杠结尾的情况下,这将不允许您转义反斜杠。要做到这一点,你必须首先用
Note however that this will not allow you to escape backslashes, in the case that you want to allow a token to end with a backslash. To do that you will have to first replace all double backslashes with
string.replaceAll("\\\\\\\\", ESCAPE_BACKSLASH)
(其中ESCAPE_BACKSLASH是一个不会的字符串发生在您的输入中)然后,在使用后面的断言进行拆分后,将ESCAPE_BACKSLASH字符串替换为未转义的反斜杠
(where ESCAPE_BACKSLASH is a string which will not occur in your input) and then, after splitting using the look-behind assertion, replace the ESCAPE_BACKSLASH string with an unescaped backslash with
token.replaceAll(ESCAPE_BACKSLASH, "\\\\")
这篇关于使用Java中的正则表达式拆分具有转义序列的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!