469,360 Members | 1,774 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,360 developers. It's quick & easy.

java replaceAll funtion ??

hello all

well i have a string like e.g. string1= " {greece [athens] : sofia[ "

and i expect ---> string2= " \{greece \[athens\] \: sofia\[ "

how can i do that i try the replaceAll() function .. can anybody give me a code example to do that..
Jan 22 '08 #1
5 12330
Stwange
126 Expert 100+
Normally I'd complain that you didn't use Google, but the regex documentation isn't exactly fantastic, so I'll let you off.
This code works (although I didn't do ALL the characters for you):
Expand|Select|Wrap|Line Numbers
  1. public class replaceAll {
  2.  
  3.     /* this code has been fully tested, and is operated by "java replaceAll your string here" from the
  4.      * terminal/command prompt.
  5.      * code provided courtesy of TheScripts.com
  6.      */
  7.     public static void main(String[] args) {
  8.     String s = "";
  9.     for (int i = 0; i < args.length; i++) {
  10.         s += args[i];
  11.     }
  12.     System.out.println(args.length == 0 ? "Please enter a string." : new replaceAll().replace(s));
  13.     }
  14.  
  15.     public static String replace(String s) {
  16.     String returned;
  17.     /* important - strings are immutable (look it up) so can't just do s.replaceAll()
  18.          * also, if you want a regex character (such as [, {) you have to escape it with "\\"
  19.      * and if you want a "\" you have to replace it with "\\\\".
  20.      */
  21.     returned = s.replaceAll("\\[","\\\\[");
  22.     returned = returned.replaceAll("\\{","\\\\{");
  23.     returned = returned.replaceAll("\\]","\\\\]");
  24.     returned = returned.replaceAll("\\}","\\\\}");
  25.     /* and so on.... */
  26.  
  27.     return returned;
  28.     }
  29. }
  30.  
Jan 22 '08 #2
thank !!!! a lot >>>>>>>>>
Jan 22 '08 #3
BigDaddyLH
1,216 Expert 1GB
Your example cries out for a regex with a capture group. The solution can be done in one line of code. Here is your example and a simpler example that demonstrates capture to replace each vowel in a string with <the vowel>. Your example is harder to read because [, ] and \ are special characters, so we descend into escaping hell.

Expand|Select|Wrap|Line Numbers
  1. public class RegexExample {
  2.     public static void main(String[] args) {
  3.         simplerExample();
  4.         yourExample();
  5.     }
  6.  
  7.     static void simplerExample() {
  8.         String input = "abcdefghijklmnopqrstuvwxyz";
  9.         String result = input.replaceAll("([aeiou])", "<$1>");
  10.         System.out.println(result);
  11.     }
  12.  
  13.     static void yourExample() {
  14.         String input = "{greece [athens] : sofia[ ";
  15.         String result = input.replaceAll("([{}\\[\\]:])", "\\\\$1");
  16.         System.out.println(result);
  17.     }
  18. }
Pattern API
Jan 22 '08 #4
Stwange
126 Expert 100+
Expand|Select|Wrap|Line Numbers
  1.         String result = input.replaceAll("([{}\\[\\]:])", "\\\\$1");
  2.  
Nice, much nicer :) I gotta get my head around regular expressions - just took out five books on Perl so that should help :)
Jan 22 '08 #5
BigDaddyLH
1,216 Expert 1GB
Nice, much nicer :) I gotta get my head around regular expressions - just took out five books on Perl so that should help :)
I have to say I hate regexes -- unreadable. They lead to the old joke of calling Perl a "write once" language because no one, least of all the author, wants to read the code.
Jan 23 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by Phil Powell | last post: by
reply views Thread by Binu jose | last post: by
2 posts views Thread by Wired Earp | last post: by
6 posts views Thread by Robbie Baldock | last post: by
458 posts views Thread by wellstone9912 | last post: by
1 post views Thread by Energizer100 | last post: by
1 post views Thread by TrippW06 | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.