Why does the following regex not work for Java? - java

I want to delete a word and all its trailing whitespace.
Here is my regex:
item.getName().replace(word + "(\\s*)?", "");
I tested this statement by running:
item.getName().replace(word, "");
This executes successfully, albeit with extra whitespaces. So the error must be due to "(\\s*)?" part. Is it because I did not escape the slash correctly? Or does Java not recognize something in that regex?

replace uses a String literal as its first argument. Use replaceAll instead

String.replace method does not take regular expressions. I believe you'd have to use replaceAll in orer to use regular expression. Also, regular expressions are a general grammar that expresses a certain pattern of String rather than a particular instances that contain certain words. You can't mix a word with a regular expression such way.

Related

Why does String.replaceAll() need so many escapes for " character?

If I have string a"b"c", but I want to get a\"b\"c\", I would naturally write
String t = "a\"b\"c\"";
t = t.replaceAll("\"", "\\\"");
However, that results in the same string, a"b"c". The correct way is
t.replaceAll("\"", "\\\\\"");
Why?
replaceAll uses regular expressions for both the pattern and the replacement - both of which require backslashes to be escaped. So the regex replacement pattern you want for the second argument is:
\\"
Now because both \ and " in Java string literals also need escaping, that means each of those characters needs an extra backslash. Add the quotes, and you've got:
"\\\\\""
which is what you've got in your source.
It's simpler if you just use String.replace which doesn't use regular expressions. That way you're only trying to provide this string (not string literal) as the second argument:
\"
After escaping and turning into a string literal, that becomes:
"\\\""
which still isn't great, but it's at least better.
An alternative is to use replaceAll but with Matcher.quoteReplacement:
t = t.replaceAll("\"", Matcher.quoteReplacement("\\\""));
Personally I'd just use replace() though. You don't want regular expression replacements, after all.

How to replace brackets in strings

I have a list of strings that contains tokens.
Token is:
{ARG:token_name}.
I also have hash map of tokens, where key is the token and value is the value I want to substitute the token with.
When I use "replaceAll" method I get error:
java.util.regex.PatternSyntaxException: Illegal repetition
My code is something like this:
myStr.replaceAll(valueFromHashMap , "X");
and valueFromHashMap contains { and }.
I get this hashmap as a parameter.
String.replaceAll() works on regexps. {n,m} is usually repetition in regexps.
Try to use \\{ and \\} if you want to match literal brackets.
So replacing all opening brackets by X works that way:
myString.replaceAll("\\{", "X");
See here to read about regular expressions (regexps) and why { and } are special characters that have to be escaped when using regexps.
As others already said, { is a special character used in the pattern (} too).
You have to escape it to avoid any confusion.
Escaping those manually can be dangerous (you might omit one and make your pattern go completely wrong) and tedious (if you have a lot of special characters).
The best way to deal with this is to use Pattern.quote()
Related issues:
How to escape a square bracket for Pattern compilation
How to escape text for regular expression in Java
Resources:
Oracle.com - JavaSE tutorial - Regular Expressions
replaceAll() takes a regular expression as a parameter, and { is a special character in regular expressions. In order for the regex to treat it as a regular character, it must be escaped by a \, which must be escaped again by another \ in order for Java to accept it. So you must use \\{.
You can remove the curly brackets with .replaceAll() in a line with square brackets
String newString = originalString.replaceAll("[{}]", "X")
eg: newString = "ARG:token_name"
if you want to further separate newString to key and value, you can use .split()
String[] arrayString = newString.split(":")
With arrayString, you can use it for your HashMap with .put(), arrayString[0] and arrayString[1]

String.replace() and regexp. Will regexp symbols in pattern affect or not?

Please correct me if I am wrong.
I want to replace substring in a string in java. And I want to use String.replace(CharSequence target, CharSequence replacement) method.
I do not use regular expressions in target substring and I think this method is a good choice.
This method will work properly even there will be special regexp symbols in target substring and it will just ignore regexp format and treat target substring as a regular string.
Am I right?
Thank you.
Yes, if you use replace the arguments will be treated as ordinary strings, not regular expressions.
If you want to replace using an regular expression you need to use replaceAll.
Yes you are correct: String.replace does not use regex. It replaces a literal with another literal.

Java string.replaceFirst problems

I'm trying to replace a part of a string. The part contains some special characters:
#L(inches)=24#
I know replaceFirst is regex driven but I can't seem to create a regular expression that matches this part in a string, any ideas?
#.*?#
This should match the entire String above.

String replace function

I have following string
String str = "replace :) :) with some other string";
And I want to replace first occurance of :) with some other string
And I used str.replaceFirst(":)","hi");
it gives following exception
"Unmatched closing ')'"
I tried using replace function but it replaced all occurance of :).
The replaceFirst method takes a regular expression as its first parameter. Since ) is a special character in regular expressions, you must quote it. Try:
str.replaceFirst(":\\)", "hi");
The double backslashes are needed because the double-quoted string also uses backslash as a quote character.
The first argument to replaceFirst() is a regular expression, not just a character sequence. In regular expressions, the parantheses have special significance. You should escape the paranthesis like this:
str = str.replaceFirst(":\\)", "hi");
Apache Jakarta Commons are often the solution for this class of problems. In this case, I would have a look at commons-lang, espacially StringUtils.replaceOnce().

Categories