-2

I have a little problem with Java string.replaceAll function with regex.

The string is like:

String s = "${start}textcontent${end}something else${start}textcontent${end}"

Now I try to use replaceall.

s.replaceAll("\\$\\{start\\}.*content.*\\$\\{end\\}", "Bla");

The result I expect is

Blasomething elseBla

But result I get is just

Bla

2 Answers2

0

You could try using Pattern.quote to escape the special characters

import java.util.regex.Pattern
s.replaceAll(Pattern.quote("${start}")+"[a-z]*"+Pattern.quote("${end}"),"Bla"));

0

Your problem is that .* is greedy, and matches like this:

${start}textcontent${end}something else${start}textcontent${end}
├───┬──┤├───────┬─────────────────────────────────┤├──┬──┤├──┬─┤
    │           │      ┌──────────────────────────────┘      │
    │        greedy    │     ┌───────────────────────────┴┘  │
    │           │      │     │         ┌─────────────────────┘
├───┴────────┤  │   ├──┴──┤  │   ├─────┴────┤
\\$\\{start\\}  .*  content  .*  \\$\\{end\\}

To fix that, make them reluctant by using .*?:

${start}textcontent${end}something else${start}textcontent${end}
├───┬──┤├─┬┤├──┬──┤├──┬─┤              ├──────┤├──┤├─────┤├────┤
    │     │    │      └──────────────────┐
    │     │    │  └┴───────────┐         │
    │     │    └────────┐      │         │
    │     └──────┐      │      │         │
├───┴────────┤   │   ├──┴──┤   │   ├─────┴────┤
\\$\\{start\\}  .*?  content  .*?  \\$\\{end\\}

The match would then repeat for the second sequence of ${start}...content...${end}.

In both cases, the second .* is matching an empty string.

Andreas
  • 154,647
  • 11
  • 152
  • 247