0

We recently converted a large project from Swing to 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. 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.

I was able to come up with this: new Alert\(*.*\R*.*\"\)\;

But it seems to also include results that end with the showAndWait() call as well.

Examples:

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

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

Thanks

Tommo
  • 867
  • 11
  • 32

1 Answers1

0

You may use a tempered greedy token solution:

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

See the regex demo and a screenshot below:

enter image description here

Pattern details:

  • \bnew - a whole word new
  • \s+ - 1 or more whitespaces
  • Alert\( - a sequence of literal chars Alert(
  • (?:(?!\.showAndWait\(\);\s*$)[\s\S])* - the tempered greedy token matching any char ([\s\S]) that is not the starting char of a .showAndWait(); + zero or more whitespaces+end of line sequence
  • "\); - a sequence of literal chars ");
Wiktor Stribiżew
  • 484,719
  • 26
  • 302
  • 397