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?

sceiler
  • 925
  • 16
  • 32
  • 2
    The regex just matches a literal `{#}`. If your `value` is `test{#}`, the `{#}` will get replaced with the number. See [this demo](https://ideone.com/Zizftn). `\\E` just stops quoting, if it is missing, the whole pattern will get quoted, that's all. – Wiktor Stribiżew May 31 '16 at 07: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 `$`. – Thomas May 31 '16 at 08:02
  • I can't see a question in the post. What are you looking for? – Wiktor Stribiżew May 31 '16 at 08: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. – sceiler May 31 '16 at 08:15
  • That makes the question a dupe of [What does the regex mean](http://stackoverflow.com/questions/22937618/reference-what-does-this-regex-mean). – Wiktor Stribiżew May 31 '16 at 08:18
  • I am not sure because I read that page but could not find information in regard to why my assumptions were wrong and nothing in regard to `{#}`. But if you think it is a duplicate then I am happy if you mark it as such or I just delete my question. – sceiler May 31 '16 at 08:36
  • Ok, I could not find exact info there either. – Wiktor Stribiżew May 31 '16 at 08:52

1 Answers1

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).

Wiktor Stribiżew
  • 484,719
  • 26
  • 302
  • 397