DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 9 of 9
  1. #1
    Join Date
    Oct 2004
    Posts
    13

    Regular Experssions: Using {} as characters in an Array?

    PHP Code:
            String text "My {burrito} has a hard shell."
    "{taco}s are yummy.";
    String output "";
    String[] replace = {"{burrito}""{taco}"};
    String[] replacer = {"taco""burrito"};

    for (
    int i 0replace.lengthi++)
    {
        
    output text.replaceAll(replace[i], replacer[i]);
    }

    System.out.println(output); 
    Gives me a compile error because I can't use { or } in the pattern matching without escaping the characters. However, I don't want to add escape sequances manually. How should I handle this?

  2. #2
    Join Date
    Feb 2004
    Posts
    541
    How come you don't want to add them manually? (so I can understand exactly what you're trying to achieve).

    Why not just do a find and replace job on all the "{" characters, replacing them with their escape characters?

  3. #3
    Join Date
    Oct 2004
    Posts
    13
    I expect to pull the values in through an external source that I have no control over.

    Java must have some method to escape strings?

    In php this would be as simple as:

    addSlashes(replace[i]);

    ...do I need to build my own class for java to do this? ::sigh::

  4. #4
    Join Date
    Feb 2004
    Posts
    541
    I just had a look through the API for the string class and I couldn't see anything. Looks like you'll have to write it yourself.

  5. #5
    Join Date
    Oct 2004
    Posts
    13
    I tried that.

    PHP Code:
        public static void main(String[] args) {
            
    String text "My {burrito} has a hard shell."
            
    "{taco}s are yummy.";
            
    String output "";
            
    String[] replace = {"{burrito}""{buffalo}"};
            
    String[] replacer = {"taco""burrito"};

            for (
    int i 0replace.lengthi++)
            {
                
    String slashed addSlashes(addSlashes(replace[i]));
                
    output text.replaceAll(slashedreplacer[0]);
                
    System.out.println(slashed);
            }
            
    System.out.println(output);
        }
        
        public static 
    String addSlashes(String theString)
        {
           
    String newString "";
           for (
    int i 0theString.length(); i++)
           {
               if (
    theString.charAt(i) == '{' || theString.charAt(i) == '}')
                   
    newString += "\\" theString.charAt(i);
               else 
    newString += theString.charAt(i);
           }
           return 
    newString;
        } 
    If I call addSlashes once the output becomes \{burrito\} which matches nothing. If I call it twice the outout becomes \\{burrito\\} which causes an error. However, if I manually type in \\{burrito\\} as the regx to match it works fine...

  6. #6
    Join Date
    Oct 2004
    Posts
    13
    Nevermind. I got my formatter to work. If some one has a better solution I'm all ears.

  7. #7
    Join Date
    Oct 2004
    Posts
    311
    the problem hereis that {} are special characters in a regex. I had this same problem myself. What I did is first call replace('{', '|') and replace('}', '}') to replace the {} with |. This is a character that has no special meaning in a regex. so now I was able to replace them without any problems.

  8. #8
    Join Date
    Oct 2004
    Posts
    13
    hahah that would have been easier than using a loop, if and character array like I did. I always manage to do things the hard way...

    I think my method would be more efficient than using replace. I only loop through the character array once. For each character you replace it will loop the entire array.

  9. #9
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    Am I missing something here ?

    You have a problem parsing a string like a regular expression ? Why not download the Apache Jakarta Regexp class ?
    eschew obfuscation

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links