正则表达式删除非14位数字的任何内容,后跟空格 [英] Regex removing anything that is not a 14-digit number followed with space
问题描述
我正试图反转此表达式:([0-9]{14} )
,
因此所有14位数字后跟一个空格.
I'm trying to invert this expression: ([0-9]{14} )
,
so all 14 digit numbers followed by a space.
我到处看,似乎最好的方法应该是使用负向超前.
但是,当我尝试将q(?!u)
应用于我的案例>> (?!([0-9]{14} ))
时,它不起作用.
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.
我在做什么错了?
我将为您提供任何建议,谢谢.
I will appreaciate any advice, thank you.
关键是要删除所有14位文本块以外的内容,同时保留这些14位文本块.
The point is to remove everything that is not a 14-digit chunk of text while preserving those 14-digit chunks.
推荐答案
如果要删除除14位数字之外的带空格的文本,请使用(\b\d{14} )|.
并替换为$1
.
If you want to delete text other than 14 digits followed with a space, use (\b\d{14} )|.
and replace with $1
.
模式匹配并捕获 (我们可以参考使用 \b
(单词边界).如果找不到此文本,则换行符以外的任何其他字符均与.
匹配,并且不会被捕获(我们无法使用反向引用来引用它).
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).
因此,当我们用向后引用$1
替换时,我们只是将匹配的14位数据块恢复为空格.
Thus, when we replace with a backreference $1
, we just restore the matched 14 digit chunk with a space.
请参见regex101.com上的 regex演示.
See the regex demo at regex101.com.
要获得更清晰的视图,请删除所有空行:Edit
> Line Operations
> Remove Empty Lines
.
To get the cleaner view, remove all empty lines: Edit
> Line Operations
> Remove Empty Lines
.
这篇关于正则表达式删除非14位数字的任何内容,后跟空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!