正则表达式删除任何不是 14 位数字后跟 space 的内容

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

我正在尝试反转这个表达式:([0-9]{14} ), 所以所有 14 位数字后跟 space.

到处都看了,好像最好的方法应该是negative lookahead.
但是当我尝试将 q(?!u) 应用到我的案例 >> (?!([0-9]{14} )) 时,它不起作用。

我做错了什么?

我会感谢任何建议,谢谢。

重点是删除所有不是 14 位文本块的内容,同时保留那些 14 位文本块。

你可以使用这个否定的前瞻:

^(?!.*[0-9]{14} )
  • 确保使用开始锚点^
  • 同样重要的是在你的模式之前使用 .* 来禁止在输入的任何地方使用

如果要删除后跟 space 的 14 位以外的文本,请使用 (\b\d{14} )|. 并替换为 </code>。</p> <p>模式匹配并<em>捕获</em>(我们可以参考用<a href="http://www.regular-expressions.info/replacebackref.html" rel="nofollow noreferrer"><strong>backreference</strong></a> in the replacement pattern) the 14-digit chunks and then a space as whole word due to <a href="http://www.regular-expressions.info/wordboundaries.html" rel="nofollow noreferrer"><code>\b (a word boundary)捕获的文本。如果找不到此文本,则除换行符外的任何字符都是与 . 匹配且未被捕获(我们无法使用反向引用来引用它)。

因此,当我们用反向引用 </code> 替换时,我们只是用 space.</p> 恢复匹配的 14 位数字块 <p>参见<a href="https://regex101.com/r/xY2pC8/1" rel="nofollow noreferrer">regex demo at regex101.com</a>。</p> <p><a href="https://i.stack.imgur.com/ztR1K.png" rel="nofollow noreferrer"><WBIMG:3702251-1.png></a></p> <p>为了获得更清晰的视图,删除所有空行:<code>Edit > Line Operations > Remove Empty Lines.