正则表达式导致StackOverflowError

时间:2016-05-23 12:24:54

标签: java regex scala parsing

我有跟随regexp

(([^'])|(''))* 

通常能够很好地解析文字

但是使用以下文字(你可以试试):

xxxxxxx_xxx_xxxxxxx=xxxxxx.xxxxxxxxxxx.xxxxxxx.xxxxxxxxxx`2[xxxxxx.xxxxxx,xxxxxx.xxxxxx]xxxxxxx_xxxxxxxxxxxxxxxx_xxxxxxxxxxx_xxxxx_xxx_xxxxxxxxxxxxxxxxx=1455544499467&xxxxxxxxx_xxxxxxxxxxxxxxxx_xxxxxxxxxxx_xxxxx_xxx_xx=xxxxxxx &xxxxxxx_xxxxxxxxxxxxxxxx_xxxxxxxxxxx_xxxxx_xxx_xxxxxxxxx=xxxxxxx_701454xx-x23x-4x31-xx75-xxx185x3xx26&xxxxxx_xxxxxxxx_xxx_xxxxxxxxxx=xxxxx&xxxxxxxxxxxxxxxxxx_xxxxxxxxxxx_xxxxx_xxx_xxxxxxxxxx=xxxxxxx&xxxxxxxx_xxx_xx=xxxxxxx_2x542x7x-7x94-4867-8819-239x732xx3x1&xxxxxxxxxxxxx_xxxxxxxxxxxxxxxx_xxxxxxxxxxx_xxxxx_xxx_xxxxxxxxxxxxxxx=xxxxx&xxxxxxxxxxxxxxxxxx_xxxxxxxxxxx_xxxxx_xxx_xxxxxxxxxxxxxx=xx-xx&xxxxxxx_xxxxxxxxxxxxxxxx_xxxxxxxxxxx_xxxxx_xxx_xxxxxxxx=7&xxxxxxxxxxxxxxxx_xxxxxxxxxxx_xxxxx_xxx_xxxxxxxxxxxxx=xxx2012x2xx.xxx.xxx:82&xxxxxxx_xxxxxxxxx_xxxxxxxxxxxxxxxx_xxxxxxxxxxx_xxxxx_xxx_xxxxxxx=11.0&xxxxxxxxx_xxxxxxxxxxxxxxxx_xxxxxxxxxxx_xxxxx_xxx_xxxxxxx=xxxx&xxxxxxxxxxxxxxxxxx_xxxxxxxxxxx_xxxxx_xxx_xxxxxxxxxxxx=xxxxxxxxx&xx_xxxxxxxxx_xxxxxxxxxxxxxxxx_xxxxxxxxxxx_xxxxx_xxx_xxxxxxx=xx 6.3&xxxxxxxxxxx_xxxxx_xxx_xxxxxxxxxxxxxxxxxxx=<xxxxxxx-xxxxx xxxxx="xxx">xxxxxx xxxxxx"></xxxxxxx><xxxxxxx-xxxx xxx-xxxxx="xx_xxxx" xxxxxxxx="xxxx" xxxx="xxxxxxxxxxx xxxx"><xxxx-xx-xxxxxx xxxxxxxxxxx="xxxx"><xxxxx>xxxxxxxxxx</xxxxx><xxxxx>xxxxx</xxxxx><xxxxx>xxx</xxxxx><xxxxx>xxxxxxxxx</xxxxx><xxxxx>xxxxxxx</xxxxx></xxxx-xx-xxxxxx></xxxxxxx-xxxx><xxxxxxx-xxxx xxx-xxxxx="xx_xxxxxxxx" xxxxxxxx="xxxxxxxx" xxxx="xx_xxxxxxxx"/><xxxxxxx-xxxx xxx-xxxxx="xx_xx_xxxxx" xxxxxxxx="xxxx" xxxx="xxxxxxx - xxxxxx xxxxxx?"><xxxx-xx-xxxxxx xxxxxxxxxxx="xxxxx"> 2</xxxxx></xxxx-xx-xxxxxx><xxxxx>xxxx</xxxxx><xxxxx>xxxxx</xxxxx></xxxx-xx-xxxxxx></xxxxxxx-xxxx><xxxxxxx-xxxx xxx-xxxxx="xx_xxxx_xxxx" xxxxxxxx="xxxx" xxxx="xx_xxxx_xxxx"></xxxxxxx><xxxxxxx-xxxx xxx-xxxxx="xx_xxxxxxx" xxxxxxxx="xxxxxx" xxxx="xx_xxxxxxx"></xxxxxxx><xxxxxxx-xxxx xxx-xxxxx="xx_xx_xxxxxxxxxxxxxx_xxxxxx" xxxxxxxx="xxxx"xxxx="xxxxxxx - xxxxxxxxxxxxxx xxxxxx"></xxxxxxx><xxxxxxx-xxxx xxx-xxxxx="xx_xxxxxx" xxxxxxxx="xxxx" xxxx="xxxxxxxxxxx xxxxxx">')

我得到StackOverflowError:

Exception in thread "main" java.lang.StackOverflowError
    at java.util.regex.Pattern$Branch.match(Pattern.java:4600)
    at java.util.regex.Pattern$GroupHead.match(Pattern.java:4658)
    at java.util.regex.Pattern$Loop.match(Pattern.java:4785)
    at java.util.regex.Pattern$GroupTail.match(Pattern.java:4717)
    at java.util.regex.Pattern$BranchConn.match(Pattern.java:4568)
    at java.util.regex.Pattern$GroupTail.match(Pattern.java:4717)
    at java.util.regex.Pattern$CharProperty.match(Pattern.java:3777)
    at java.util.regex.Pattern$GroupHead.match(Pattern.java:4658)

可能是什么问题? 由于字符串的长度太长(2030个符号),是否可能出现此类异常? 或者我在这里有一些特殊的符号导致这个错误?

赞赏任何想法

1 个答案:

答案 0 :(得分:2)

首先,您不需要内部捕获组。重复捕获组仅捕获最后一次出现,因此,它们在Java中无用。

接下来,你可以unroll这个正则表达式,以便它线性匹配(没有交替占用大量资源):

[^']*(?:''[^']*)*

请参阅updated regex演示。

模式详情

  • [^']* - 匹配'
  • 以外的零个或多个字符
  • (?:''[^']*)* - 匹配零个或多个序列:
    • '' - 两个单撇号
    • [^']* - 除'
    • 以外的零个或多个字符