Regex pattern to replace the wrong placeholders with blank

Viewed 119

I want to replace a String which contains of placeholders which should only consists on digit.

For ex - this is the test string {0} with correct placeholder {1}.

Valid placeholders - {digits}
Invalid Placeholders - alphanumeric, empty, digit with whitespace, character with whitespace

Can anyone help me with a regex. I tried this but its not working -

\{((([^0-9]).+?)|(.([^0-9])+?))\}

Thank in advance!

1 Answers

You can use

\{(?!\d+\})[^{}]*\}

See the regex demo

Details

  • \{ - a { char
  • (?!\d+\}) - a negative lookahead that fails the match if there are 1 or more digits and then a } char immediately to the right of the current location
  • [^{}]* - 0 or more chars other than { and }
  • \} - a } char.
Related