• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

replacing character at a beginning of a string with another at its end

 
Ranch Hand
Posts: 100
VI Editor Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Greetings and Salutations!

Please help to replace string "#xyz"with "xyz,".
Running

public class ReplaceChar {
public static void main(String[] args) {
String str = "#xyz";
String result = str.replaceAll("#(.*)", "\1,");
System.out.println(result);
}
}

outputs "," by itself.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you look at the Javadocs for String.replaceAll, they refer you to Matcher.replaceAll...

...which refers you to Matcher.appendReplacement...

...which tell you that backslashes are not used to refer to a captured group. You need $1 instead.

[Note that if it was a backslash you needed, it would have to be \\1, because backslash has a special meaning in a string literal]
 
Igor Mechnikov
Ranch Hand
Posts: 100
VI Editor Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you so much!
I really appreciate the references as well.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic