替换字符串中的反向引用语法(为什么是美元符号?) [英] Backreferences Syntax in Replacement Strings (Why Dollar Sign?)

查看:349
本文介绍了替换字符串中的反向引用语法(为什么是美元符号?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中,似乎在其他几种语言中,模式中的反向引用前面都有反斜杠(例如 \1 \\ \\ 2 \3 等),但在替换字符串中,它们前面有一个美元符号(例如 $ 1 $ 2 $ 3 ,还 $ 0 )。

In Java, and it seems in a few other languages, backreferences in the pattern are preceded by a backslash (e.g. \1, \2, \3, etc), but in a replacement string they preceded by a dollar sign (e.g. $1, $2, $3, and also $0).

这里有一个片段来说明:

Here's a snippet to illustrate:

System.out.println(
    "left-right".replaceAll("(.*)-(.*)", "\\2-\\1") // WRONG!!!
); // prints "2-1"

System.out.println(
    "left-right".replaceAll("(.*)-(.*)", "$2-$1")   // CORRECT!
); // prints "right-left"

System.out.println(
    "You want million dollar?!?".replaceAll("(\\w*) dollar", "US\\$ $1")
); // prints "You want US$ million?!?"

System.out.println(
    "You want million dollar?!?".replaceAll("(\\w*) dollar", "US$ \\1")
); // throws IllegalArgumentException: Illegal group reference

问题:


  • 是否使用 $ 进行Java特有的替换字符串的反向引用?如果没有,用什么语言开始呢?什么口味使用它和什么不使用?

  • 为什么这是一个好主意?为什么不坚持使用相同的模式语法?这不会导致更具凝聚力和更容易学习的语言吗?


    • 如果上面的语句1和4是正确的而不是2和3,语法是否会更加简化?

    • Is the use of $ for backreferences in replacement strings unique to Java? If not, what language started it? What flavors use it and what don't?
    • Why is this a good idea? Why not stick to the same pattern syntax? Wouldn't that lead to a more cohesive and an easier to learn language?
      • Wouldn't the syntax be more streamlined if statements 1 and 4 in the above were the "correct" ones instead of 2 and 3?

      推荐答案


      使用$作为Java特有的替换字符串中的反向引用吗?

      Is the use of $ for backreferences in replacement strings unique to Java?

      否。 Perl使用它,Perl肯定早于Java的 Pattern 类。 Java的正则表达式支持是根据Perl正则表达式明确描述的。

      No. Perl uses it, and Perl certainly predates Java's Pattern class. Java's regex support is explicitly described in terms of Perl regexes.

      例如: http://perldoc.perl.org/perlrequick.html#Search-and-replace


      为什么这是一个好主意?

      Why is this a good idea?

      很明显你不认为这是个好主意!但是,一个好主意的一个原因是使Java搜索/替换支持(更多)与Perl兼容。

      Well obviously you don't think it is a good idea! But one reason that it is a good idea is to make Java search/replace support (more) compatible with Perl's.

      还有另一个可能的为什么 $ 可能被视为比 \ 更好的选择。也就是说, \ 必须在Java字符串文字中写为 \\

      There is another possible reason why $ might have been viewed as a better choice than \. That is that \ has to be written as \\ in a Java String literal.

      但所有这些都是纯粹的猜测。在作出设计决定时,我们都不在房间里。最终,为什么他们以这种方式设计替换String语法并不重要。这些决定已经制定并具体设置,任何进一步的讨论都纯粹是学术性的......除非您恰好为Java设计新语言或新的正则表达式库。

      But all of this is pure speculation. None of us were in the room when the design decisions were made. And ultimately it doesn't really matter why they designed the replacement String syntax that way. The decisions have been made and set in concrete, and any further discussion is purely academic ... unless you just happen to be designing a new language or a new regex library for Java.

      这篇关于替换字符串中的反向引用语法(为什么是美元符号?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆