Skip to content
Advertisement

Netbeans Check Regular Expression

Here is an example of a very simple method. We take “foo bar”, split, replaceAll, and print.

public static void main(String[] args){
  String string="foo bar";
  String[] array=string.split(" ");
  String string2=string.replaceAll(".* ","");
  System.out.println(array[0]);
  System.out.println(string2);
}

NetBeans is claiming this is a problem and giving me a warning on split and replaceAll.

enter image description here

I can’t seem to find a way to disable this via @annotation, and information about this issue is sparse on the ‘net. What is the intended effect of this warning?

Advertisement

Answer

The feature “Check Regular Expression” was recently implemented in NetBeans 12.5.

It is formally documented in Apache NetBeans issue NETBEANS-5661 Testing for valid values of a Regular Expression in the IDE which has the DescriptionAdding a feature for testing the valid values for a Regular Expression in the IDE itself“.

It is also mentioned in the Release Notes for NetBeans 12.5:

[NETBEANS-5661] – Added regular expression window and hint: https://github.com/apache/netbeans/pull/2953

In the Attachments section of NetBeans Issue 5661 there is a link named RegexDemo2.gif which gives a demo of the new functionality. It allows you to test regular expressions within your Java source, in a Check Regular Expressions window.

To configure your preference for this feature, navigate to Tools > Options > Editor > Hints, then select Java from the Language drop list. As shown in the screen shot below, you can then:

  • Turn the feature off completely, by unchecking the Check Regular Expression entry.

  • Alternatively, generate an Error, Warning, or Warning on Current Line by selecting the appropriate Show As entry.

    Options window

You can also navigate to that configuration screen directly from the relevant line in your code using tooltips:

tooltips

For trivial regular expressions (as in your sample code) this new feature may be a minor irritation, in which case turn it off. But for code which contains many regular expressions, or some complex regular expressions, I think this feature could be useful and productive. You might perhaps generate the warnings during development and for code reviews, but then turn it off prior to final deployment.

One final point: those warning/error messages do not necessarily imply that there is anything wrong with your regular expression at all. Instead, the feature serves to highlight the existence of regular expressions in your code, and provides a means to easily test them.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement