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
Questions:
- 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?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…