简体   繁体   English

正则表达式模式用空白替换错误的占位符

[英]Regex pattern to replace the wrong placeholders with blank

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}.例如 - this is the test string {0} with correct placeholder {1}.

Valid placeholders - {digits}有效占位符 - {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!预先感谢!

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 (?!\\d+\\}) - 如果有 1 个或多个数字,则匹配失败的负前瞻,然后在当前位置的右侧有一个}字符
  • [^{}]* - 0 or more chars other than { and } [^{}]* - 除{}之外的 0 个或多个字符
  • \\} - a } char. \\} - 一个}字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM