`
renzhen
  • 浏览: 247946 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

在netbeans替换中使用正则表达式

阅读更多

  最近在netbeans的替换功能中使用正则表达式中的反向引用,总是不成功,网上找资料也不容易,找了很久,终于在oracle的blog找到了答案。

  原来netbeans的替换支持反向引用,只是他是使用$0、$1进行反向引用的,如果习惯了使用\1、\2的,要改改了。

 

   原文如下:

We've recently added regular expression support into the Find dialog in NetBeans (the Ctrl-F one). Works like charm for both find and replace. But not for all users. Several days after commit we've got user feedback: "But you don't support back-references". "Sure we do". "They don't work for me".

So the problem is that there are two types of back-references. One type which is denoted as $1 and one as \\1. NetBeans uses the $1 one which is used normally in Java. However some other parts of the world are using \\1.

So how do we tell users which one they have to use? Nobody really reads documentation so perhaps we need to tell users in the dialog. If they try to use \\1, we should politely ask them if they didn't want to use $1. Sigh, why can't there be just one standard for everything? :-)

For those who do not know what back-references are, here is my explanation:

Back-references are a very useful when replacing some text. You have the following text: "Hello, world". Now, you can do following:



If you replace the text like this in NetBeans, the result is: "Farewell, world". One replace is not much useful, but imagine if you have a long file where you need to do many replaces and a the file's more complicated. You can use $2, $3, etc. for next elements in parentesis if you want to replace more of them at once. $0 is used for the whole searched string.

This feature can certainly save you a lot of time you would spend by manual editing. And you can impress other people with it as well :-)
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics