简体   繁体   English

Java Eclipse Regex多行搜索

[英]Java Eclipse Regex Multiline Search

We recently converted a large project from Swing to JavaFX. 我们最近将一个大型项目从Swing转换为JavaFX。 A conversion program was written to automate some of this process which saved a lot of time, but also left us with some issues. 编写了一个转换程序来自动执行此过程,从而节省了很多时间,但同时也给我们带来了一些问题。 One of which is we have some Alerts that never have showAndWait() called. 其中之一是我们有些警报从未调用showAndWait() I was hoping to put a Regex together to find all instances in our project that meet the following criteria: 我希望将正则表达式放在一起,以查找项目中满足以下条件的所有实例:

Starts with new Alert( 从新警报开始

End with "); “)结尾

And potentially contains 0 or multiple line feeds along with any characters in between the parenthesis. 并且可能包含0个或多个换行符以及括号之间的任何字符。

I was able to come up with this: new Alert\\(*.*\\R*.*\\"\\)\\; 我想出了这个: new Alert\\(*.*\\R*.*\\"\\)\\;

But it seems to also include results that end with the showAndWait() call as well. 但是它似乎还包括以showAndWait()调用结尾的结果。

Examples: 例子:

new Alert(AlertType.INFORMATION, "This alert should not be matched").showAndWait();

new Alert(AlertType.INFORMATION, "This alert should be matched");

Thanks 谢谢

You may use a tempered greedy token solution: 您可以使用经过调整的贪婪令牌解决方案:

\bnew\s+Alert\((?:(?!\.showAndWait\(\);\s*$)[\s\S])*"\);

See the regex demo and a screenshot below: 请参阅下面的正则表达式演示和屏幕截图:

在此处输入图片说明

Pattern details : 图案细节

  • \\bnew - a whole word new \\bnew new
  • \\s+ - 1 or more whitespaces \\s+ -1个或多个空格
  • Alert\\( - a sequence of literal chars Alert( Alert\\( -文字字符Alert(
  • (?:(?!\\.showAndWait\\(\\);\\s*$)[\\s\\S])* - the tempered greedy token matching any char ( [\\s\\S] ) that is not the starting char of a .showAndWait(); (?:(?!\\.showAndWait\\(\\);\\s*$)[\\s\\S])* -与任何不是char的起始char的char( [\\s\\S] )匹配的调和贪婪令牌.showAndWait(); + zero or more whitespaces+end of line sequence +零个或多个空格+行尾
  • "\\); - a sequence of literal chars "); "\\); -文字字符序列");

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

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