简体   繁体   English

未封闭字符类(正则表达式)

[英]Unclosed Character Class (Regex)

So, I have this semi-complex regex that is searching for all text in between two strings, then replacing it. 因此,我有一个半复杂的正则表达式,它在两个字符串之间搜索所有文本,然后替换它。

My search regex for this is: 我的搜索正则表达式是:

(jump *[A-Z].*)(?:[^])*?([A-Z].*:)

This gives an Unclosed Character Class on the final closing bracket, which I have been struggling to solve. 这在最终的结束括号中提供了一个Unclosed字符类,我一直在努力解决。 The regex seems to work as intended on RegexR ( http://regexr.com/?38k63 ) regex似乎可以在RegexR( http://regexr.com/?38k63 )上正常工作

Could anyone provide some help or insight? 谁能提供帮助或见解?

Thanks in advance. 提前致谢。

The error is at here: 错误在这里:

(jump *[A-Z].*)(?:[^])*?([A-Z].*:)
                   ^

In character class ^ is still a special character. 在字符类中, ^仍然是特殊字符。 It usually negates other characters when you place there. 当您放置其他字符时,它通常会否定其他字符。 So escape it with \\\\ in Java. 因此,在Java中使用\\\\对其进行转义。

Different regex engines will treat [^] differently. 不同的正则表达式引擎将对[^]不同的处理。 Some will assume that it's the beginning of a negative character class excluding ] and any characters up to the next ] in the pattern, (eg [^][] will match anything except ] and [ ). 有人会假设这是一个否定字符类的开始,不包括]和该模式中直到下一个]任何字符(例如[^][]将匹配除][之外的任何字符)。 Other engines will treat as a empty negative character class (which will match anything). 其他引擎将被视为空的否定字符类(它将匹配任何内容)。 This is why some regex engines will work, and others report it as an error. 这就是为什么某些正则表达式引擎可以运行,而另一些则将其报告为错误的原因。

If you meant for it to match a literal ^ character, you'll have to escape it like this: 如果要让它与文字^字符匹配,则必须像这样转义它:

(jump *[A-Z].*)(?:[\^])*?([A-Z].*:)

Or better yet, just remove it from the character class (you'll still have to escape it because ^ has special meaning outside of a character class, too): 或者更好的是,只需将其从字符类中删除(您仍然必须对其进行转义,因为^在字符类之外也具有特殊含义):

(jump *[A-Z].*)(?:\^)*?([A-Z].*:)

Or if you meant for it to match everything up to the next [AZ].*: , try a character class like this: 或者,如果您要让它匹配下一个[AZ].*: ,请尝试这样的字符类:

(jump *[A-Z].*)(?:[\s\S])*?([A-Z].*:)

And of course, because this is Java, don't forget that you'll need to escape the all the \\ characters in any string literals. 当然,因为这是Java,所以不要忘记您需要对任何字符串文字中的所有\\字符进行转义。

Problem seems here in use of [^] : 在使用[^]

(jump *[A-Z].*)(?:[^])*?([A-Z].*:)
                   ^
-------------------|

Try this regex instead: 尝试使用此正则表达式:

(jump *[A-Z].*)[\\s\\S]*?([A-Z].*:)

OR this: 或这个:

(?s)(jump *[A-Z].*).*?([A-Z].*:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM