0

My input is the following string:

"<div>content <label class=\"ph\">$data</label> <label class=\"ph\">$data</label> <label class=\"ph\">$data</label> more content</div>" (Where instead of $data there might be any string)

And my required output is: "<div>content REPLACED <label class=\"ph\">$data</label> <label class=\"ph\">$data</label> more content</div>"

Basically, replacing just the first occurrence of a pattern like:
String regex = <label class=\"ph\">.*</label>

But when trying to use: Pattern.compile(regex).matcher(contentInput).replaceFirst("");

I'm getting: "<div>content REPLACED more content</div>"

Any Ideas of the correct regex to use, or any other idea? Thanks!

Omered
  • 41
  • 4

1 Answers1

0

The problem is with slash try using this pattern:

String regex = "<label class=\\\"ph\"\\>.*</label>"

This will match: <label class=\"pH"\>ANYTHING</label>

Where \\ is used to print or match \ And \" is used to print or match "