Home > OS >  Regex to replace the character after pattern match
Regex to replace the character after pattern match

Time:01-21

For my Java app, i have a string that looks like:

value1: *sample", test
test: "test"
newtest: *newtest"

I need to match the character " when the string starts with *.
Tried the regex: "(?!.*") But this selected all the " in the input.
Was planning to replaceAll(regex, "") to remove the character.

Desired Output:

value1: *sample, test
test: "test"
newtest: *newtest

How do i get this output?

CodePudding user response:

You can use

.replaceAll("(\\*\\w )\"", "$1")

Details:

  • (\*\w ) - Group 1 ($1 refers to the text captured in this group)
  • " - a " char (just matched, not captured, so eventually removed).
  • Related