Hi...
Thanks Jeff for your input....
 
I believe i found a solution java developer almanac....
 
You see the solution was suppose to be generic for this reasons.
1. I read a src file and from each line of text i have a line number.
2. I tokenize the line and then find the column where eachh word starts.
    e.g to generate a output file with line no, char no
3. the token then goes through mathes of keywords, identifier and punctuation
    test.
4. But then I discovered that when you have a string or text like the following if(){
   and you compile a pattern on that it fails since (,),{ are all part of regular
   _expression_ syntax.
 
The solution for your perusal if interested
 
import java.util.regex.Pattern;
public class test{
  public static void main(String[] args){
   String testString = "This{is{a{test";
   System.out.println("Original String > "+testString);
   System.out.println("Escaped  String > "+escapeRE(testString));
      System.out.println("Replacer String > "+testString.replaceAll(escapeRE("{")," "));
  }
  // Returns a pattern where all punctuation characters are escaped.
  public static String escapeRE(String str) {
    Pattern escaper = Pattern.compile("([^a-zA-z0-9])");
    return escaper.matcher(str).replaceAll("\\\\$1");
  }
}
 
Thanx again
D


Jeff Mutonho <[EMAIL PROTECTED]> wrote:

On 5/11/06, Jeff Mutonho <[EMAIL PROTECTED]>wrote:
> On 5/11/06, Java Mad <[EMAIL PROTECTED]>wrote:
> >
> > Hi,
> >
> > Does any one know how to replace all { with \{
> >
> > I tried String.replaceAll("{","\{") only to find exceptions since { is part
> > of the regular _expression_ syntax stuff.
> >
> > What i want to achieve with this is to replace all {,(,[,] which is part of
> > a String with \{,\(,\[,\]
> >
> > e.g "if()"
> > will be converted to if\(\)
> >
> > I need this for a Lexical Analyser
> >
> Try escaping the '{' with 2 backslashes
>

As in
String oldS="foo{";
System.out.println(oldS);
String newS= oldS.replaceAll("\\{","\\\\{");
System.out.println(newS);

will give you

foo{
foo\{




Jeff Mutonho

GoogleTalk : ejbengine
Skype : ejbengine
Registered Linux user number 366042


New Yahoo! Messenger with Voice. Call regular phones from your PC and save big.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CTJUG Forum" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/CTJUG-Forum
For the ctjug home page see http://www.ctjug.org.za
-~----------~----~----~----~------~----~------~--~---

Reply via email to