简体   繁体   English

Netbeans 检查正则表达式

[英]Netbeans Check Regular Expression

Here is an example of a very simple method.这是一个非常简单的方法的例子。 We take "foo bar", split, replaceAll, and print.我们采用“foo bar”、拆分、replaceAll 和打印。

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 . NetBeans 声称这是一个问题,并在splitreplaceAll上给我一个警告。

在此处输入图片说明

I can't seem to find a way to disable this via @annotation, and information about this issue is sparse on the 'net.我似乎无法通过@annotation 找到禁用此功能的方法,并且有关此问题的信息在 'net. What is the intended effect of this warning?此警告的预期效果是什么?

The feature "Check Regular Expression" was recently implemented in NetBeans 12.5.最近在 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 Description " Adding a feature for testing the valid values for a Regular Expression in the IDE itself ".它正式记录在 Apache NetBeans 问题NETBEANS-5661 测试 IDE 中正则表达式的有效值中,描述为添加用于在 IDE 本身中测试正则表达式的有效值的功能”。

It is also mentioned in the Release Notes for NetBeans 12.5 : NetBeans 12.5发行说明中也提到了这一点:

[NETBEANS-5661] - Added regular expression window and hint: https://github.com/apache/netbeans/pull/2953 [NETBEANS-5661] - 添加了正则表达式窗口和提示: 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.在 NetBeans 问题 5661 的附件部分中有一个名为RegexDemo2.gif的链接,它提供了新功能的演示。 It allows you to test regular expressions within your Java source, in a Check Regular Expressions window.它允许您在“检查正则表达式”窗口中测试 Java 源代码中的正则表达式

To configure your preference for this feature, navigate to Tools > Options > Editor > Hints , then select Java from the Language drop list.要配置您对此功能的首选项,请导航到工具 > 选项 > 编辑器 > 提示,然后从语言下拉列表中选择Java 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.或者,通过选择适当的“显示为”条目,在当前行上生成错误警告警告

    选项窗口

You can also navigate to that configuration screen directly from the relevant line in your code using 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.相反,该功能用于突出显示代码中正则表达式的存在,并提供一种轻松测试它们的方法。

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

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