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

别来无恙 提交于 2019-12-23 02:42:07

问题


I'm trying to invert this expression: ([0-9]{14} ), so all 14 digit numbers followed by a space.

I looked everywhere, and it seems that the best way should be using negative lookahead.
But when I try apply q(?!u) to my case >> (?!([0-9]{14} )), it doesn't work.

What am I doing wrong?

I will appreaciate any advice, thank you.

The point is to remove everything that is not a 14-digit chunk of text while preserving those 14-digit chunks.


回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/36696455/regex-removing-anything-that-is-not-a-14-digit-number-followed-with-space

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!