-
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 = 0; i < replace.length; i++)
{
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?
-
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?
-
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::
-
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.
-
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 = 0; i < replace.length; i++)
{
String slashed = addSlashes(addSlashes(replace[i]));
output = text.replaceAll(slashed, replacer[0]);
System.out.println(slashed);
}
System.out.println(output);
}
public static String addSlashes(String theString)
{
String newString = "";
for (int i = 0; i < theString.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...
-
Nevermind. I got my formatter to work. If some one has a better solution I'm all ears.
-
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.
-
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.
-
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
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
|
Bookmarks