1

I have this code snippet of a code base I am supposed to maintain.

String number = "1";
String value = "test";
String output = "";

output = value.replaceAll("\\Q{#}", number);

The value of output stays as "test" and I can only guess what this code is supposed to do: the value of numbershould be appended to whatever is in value. Maybe something like this: test1 or replace the value with the number entirely.

I found out that \\Q is the regex option to quote everything until \\E but there is no \\E. Anyway it is not doing anything at all and I am wondering if I oversee something?

7
  • 2
    The regex just matches a literal {#}. If your value is test{#}, the {#} will get replaced with the number. See this demo. \\E just stops quoting, if it is missing, the whole pattern will get quoted, that's all. Commented May 31, 2016 at 7:57
  • AFAIK # is no special character in regex so assuming Java regex understands \Q it would look for the literal string {#} which is not present in value. Besides that your guess is wrong: replaceAll replaces the match with number - if you want to append something at the end you'd need match the end of the input, e.g. just using the expression $. Commented May 31, 2016 at 8:02
  • I can't see a question in the post. What are you looking for? Commented May 31, 2016 at 8:04
  • @WiktorStribiżew this is exactly what I needed. An explanation what this thing exactly does because my guess was totally wrong. I thought # would be a special character, in this case a token or something similar which would get replaced. Thanks. If you put your comment as an answer I will mark it as correct. @@Thomas thanks for clarifying and the explanation. Commented May 31, 2016 at 8:15
  • That makes the question a dupe of What does the regex mean. Commented May 31, 2016 at 8:18

1 Answer 1

1

Your regex just matches a literal {#}. It is true that after \Q the pattern is considered to have literal symbols (all the symbols after \Q get "quoted" or "escaped"), and \E stops this escaping/quoting, and if it is missing, the whole pattern will get quoted/escaped.

If your value variable holds test{#} value, the {#} will get replaced with the number.

See this demo:

String number = "1";
String value = "test{#}";
String output = "";
output = value.replaceAll("\\Q{#}", number);
System.out.println(output); // => test1

Note that without \Q, your regex ({#}) would throw a java.util.regex.PatternSyntaxException: Illegal repetition error because Java regex engine is not "smart" enough to disambiguate the braces (PCRE, JS, .NET can easily guess that since there is no number inside, it is not a limiting/bound quantifier).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.