gpt4 book ai didi

java - 无法使用正则表达式匹配字符串

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:16:44 25 4
gpt4 key购买 nike

我正在研究一些正则表达式,我想知道为什么这个正则表达式

"(?<=(.*?id(( *)=)\\s[\"\']))g"

不匹配字符串

<input id = "g" />

在 Java 中?

最佳答案

Java.util.regex 不支持无限后视,如 RegexBuddy 中所述:

The bad news is that most regex flavors do not allow you to use just any regex inside a lookbehind, because they cannot apply a regular expression backwards. Therefore, the regular expression engine needs to be able to figure out how many steps to step back before checking the lookbehind.

从文档中添加一点说明:

Therefore, many regex flavors, including those used by Perl and Python, only allow fixed-length strings. You can use any regex of which the length of the match can be predetermined. This means you can use literal text and character classes. You cannot use repetition or optional items. You can use alternation, but only if all options in the alternation have the same length.

Some regex flavors, like PCRE and Java support the above, plus alternation with strings of different lengths. Each part of the alternation must still have a finite maximum length. This means you can still not use the star or plus, but you can use the question mark and the curly braces with the max parameter specified. These regex flavors recognize the fact that finite repetition can be rewritten as an alternation of strings with different, but fixed lengths. Unfortunately, the JDK 1.4 and 1.5 have some bugs when you use alternation inside lookbehind. These were fixed in JDK 1.6.

关于java - 无法使用正则表达式匹配字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3876676/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com