java replaceAll funtion ??

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • chriskaza81
    New Member
    • Nov 2007
    • 52

    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..
  • Stwange
    Recognized Expert New Member
    • Aug 2007
    • 126

    #2
    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):
    Code:
    public class replaceAll {
    
        /* this code has been fully tested, and is operated by "java replaceAll your string here" from the
         * terminal/command prompt.
         * code provided courtesy of TheScripts.com
         */
        public static void main(String[] args) {
    	String s = "";
    	for (int i = 0; i < args.length; i++) {
    	    s += args[i];
    	}
    	System.out.println(args.length == 0 ? "Please enter a string." : new replaceAll().replace(s));
        }
    
        public static String replace(String s) {
    	String returned;
    	/* important - strings are immutable (look it up) so can't just do s.replaceAll()
             * also, if you want a regex character (such as [, {) you have to escape it with "\\"
    	 * and if you want a "\" you have to replace it with "\\\\".
    	 */
    	returned = s.replaceAll("\\[","\\\\[");
    	returned = returned.replaceAll("\\{","\\\\{");
    	returned = returned.replaceAll("\\]","\\\\]");
    	returned = returned.replaceAll("\\}","\\\\}");
    	/* and so on.... */
    	
    	return returned;
        }
    }

    Comment

    • chriskaza81
      New Member
      • Nov 2007
      • 52

      #3
      thank !!!! a lot >>>>>>>>>

      Comment

      • BigDaddyLH
        Recognized Expert Top Contributor
        • Dec 2007
        • 1216

        #4
        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.

        [CODE=Java]public class RegexExample {
        public static void main(String[] args) {
        simplerExample( );
        yourExample();
        }

        static void simplerExample( ) {
        String input = "abcdefghijklmn opqrstuvwxyz";
        String result = input.replaceAl l("([aeiou])", "<$1>");
        System.out.prin tln(result);
        }

        static void yourExample() {
        String input = "{greece [athens] : sofia[ ";
        String result = input.replaceAl l("([{}\\[\\]:])", "\\\\$1");
        System.out.prin tln(result);
        }
        }[/CODE]

        Pattern API

        Comment

        • Stwange
          Recognized Expert New Member
          • Aug 2007
          • 126

          #5
          Originally posted by BigDaddyLH
          Code:
                  String result = input.replaceAll("([{}\\[\\]:])", "\\\\$1");
          Nice, much nicer :) I gotta get my head around regular expressions - just took out five books on Perl so that should help :)

          Comment

          • BigDaddyLH
            Recognized Expert Top Contributor
            • Dec 2007
            • 1216

            #6
            Originally posted by Stwange
            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.

            Comment

            Working...