Regex removing anything that is not a 14-digit number followed with space

三世轮回 提交于 2019-12-08 04:55:24

If you want to delete text other than 14 digits followed with a space, use (\b\d{14} )|. and replace with $1.

The pattern matches and captures (we can refer to the text captured with a backreference in the replacement pattern) the 14-digit chunks and then a space as whole word due to \b (a word boundary). If this text is not found, any character other than a newline is matched with . and is not captured (we cannot refer to it with a backreference).

Thus, when we replace with a backreference $1, we just restore the matched 14 digit chunk with a space.

See the regex demo at regex101.com.

To get the cleaner view, remove all empty lines: Edit > Line Operations > Remove Empty Lines.

You can use this negative lookahead:

^(?!.*[0-9]{14} )
  • Make sure you use start anchor ^
  • Also important to use .* before your pattern to disallow this anywhere in input
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!