Java正则表达式的后视组没有明显的最大长度 [英] Look-behind group for a Java regular expression does not have an obvious maximum length near

查看:1638
本文介绍了Java正则表达式的后视组没有明显的最大长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想:

当我得到一个大字符串时,我需要使用Java中的正则表达式在其中查找内容,以使用以下公式分隔:

When I get a big string I need to find stuff in it using a regular expression in Java to separate using the following formulas:


  • 如果一行(在\ n之后)有超过1000个字符,请检查奇数'。

  • 然后在1000和1001个字符之间添加一个连续字符串'\ n ||'

  • 如果1000和1001个字符是''(plsql的转义字符),则将其插入1001和1002之间

无论如何我做了这个正则表达式:

Anyway I made this regular expression:

"\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

无论如何,我似乎得到了以下错误。

Anyway, it seems I get the folowing error.


线程main中的异常java.util.regex.PatternSyntaxException:Look-behind组在索引161附近没有明显的最大长度

Exception in thread "main" java.util.regex.PatternSyntaxException: Look-behind group does not have an obvious maximum length near index 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}而不是 * 吗?

Why does it do that? Did I not make the limitation by using {0,1001} instead of *?

推荐答案

Java的正则表达式引擎不支持可变长度外观。这意味着当后面的长度不固定时,引擎将抛出此异常。你看后面的长度是可变的,因此你会得到这个例外。

Java's regex engine does not support variable length look behind. That means that when the length of the look behind is not fixed the engine will throw this exception. Your look behind's length is variable, thus you get this exception.

Java正则表达式错误 - Look-behind组没有明显的最大长度

这篇关于Java正则表达式的后视组没有明显的最大长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆