我想:
当我得到一个大字符串时,我需要使用Java中的正则表达式在其中查找内容,以使用以下公式分隔:
'
后是否包含第1000个字符。无论如何,我做了这个正则表达式:
"\n(?<kiloCharacters>[^\n]{1000})(?<=(?<newLine>\n)(?<pairsAndText>[^'\n]{0,1001}|[^\n']{0,1001}'[^\n']{0,1001}'[^\n']{0,1001}){0,1001}(?<oddComa>')(?<text>[^\n']{0,1001}))(?(?<=')(?!'))"
让我解释一下:
"\n(?<kiloCharacters>[^\n]{1000}) --> Newline and 1000 characters
(?<= --> Let's look behind to check if we have an odd number of '
(?<newLine>\n) --> Start from new line
(?<pairsAndText> --> All pairs of '
[^'\n]{0,1001} --> Eighter 0 '
| --> or
[^\n']{0,1001}'[^\n']{0,1001}'[^\n']{0,1001}){0,1001} --> (text* ' text* ' text* )*
(?<oddComa>') --> Last comma
(?<text>[^\n']{0,1001}) --> Text after that comma
) --> End of actual looking behind
(?(?<=')(?!'))" --> This part check if we are inside an escaped character '' as we can not concat stuff between here
无论如何,我似乎得到了以下错误。
线程“main”中的异常java.util.regex.PatternSyntaxException:Look-behind组在索引161附近没有明显的最大长度
(?<kiloCharacters>[^
]{1000})(?<=(?<newLine>
)(?<pairsAndText>[^'
]{0,1001}|[^
']{0,1001}'[^
']{0,1001}'[^
']{0,1001}){0,1001}(?<oddComa>')(?<text>[^
']{0,1001}))(?(?<=')(?!'))
^
at java.util.regex.Pattern.error(Unknown Source)
at java.util.regex.Pattern.group0(Unknown Source)
at java.util.regex.Pattern.sequence(Unknown Source)
at java.util.regex.Pattern.expr(Unknown Source)
at java.util.regex.Pattern.compile(Unknown Source)
at java.util.regex.Pattern.<init>(Unknown Source)
at java.util.regex.Pattern.compile(Unknown Source)
at java.lang.String.replaceAll(Unknown Source)
为什么这样做?我没有使用{0,1001}代替*
吗?
答案 0 :(得分:1)
Java的正则表达式引擎不支持可变长度外观。这意味着当后面的长度不固定时,引擎将抛出此异常。你看后面的长度是可变的,因此你会得到这个例外。
Java regex error - Look-behind group does not have an obvious maximum length