473,372 Members | 1,055 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 software developers and data experts.

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 12558
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

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

Similar topics

0
by: Phil Powell | last post by:
URL: http://valsignalandet.com/cookiegrab.php?name=valIdentifier This page produces a cookie value and sends it back as HTML using PHP and Javascript to obtain it. This URL will be used as the...
0
by: Binu jose | last post by:
Hi all i am trying to replace a char sequence starting with -- and ending with \r\n using String.replaceAll() function. How will be the regular expression for that. Thanks Binu Jose
2
by: Wired Earp | last post by:
I've had some luck using string values "\t" "\n" and "\r" to insert tabs, newlines and carriagereturn textnodes into a document, but I can't *read* these nodes, at least not by analyzing the...
6
by: Robbie Baldock | last post by:
Hi - I'm a bit of a newbie to the world of XSLTs but am trying to call a Java method on a parameter passed into an XSLT but am having problems. I've stripped the XSLT down to its bare bones: ...
458
by: wellstone9912 | last post by:
Java programmers seem to always be whining about how confusing and overly complex C++ appears to them. I would like to introduce an explanation for this. Is it possible that Java programmers...
5
by: Lee Xuzhang | last post by:
/* from SICP -- Exercise 4.21: ((lambda (n) ((lambda (fact) (fact fact n)) (lambda (ft k) (if (= k 1) 1 (* k (ft ft (- k 1))))))) 10) */
1
by: Energizer100 | last post by:
In the code below, I'm trying to convert a sentence into shorthand, meaning I take away all the vowels, plus I convert "and" to "&", "to" to "2", "you" to "U", and "for" to "4". public static...
4
by: sukatoa | last post by:
This was my first time to encouter this kind of exception.... that exception appears when i invoked the the method below. private final String encrypting(String enc){ int...
1
by: TrippW06 | last post by:
I working on a program to analyze a string and tell the user how may of each char is in the sentence. I have it sorta working but cant fidgure it out. I get a weird last line and my method to remove...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.