简体   繁体   English

正则表达式:除某些模式外的所有内容

[英]Regex: Everything except some pattern

I have a string: 我有一个字符串:

foo bar 富吧
foo1 #9 0x103806f4 bar1 foo1#9 0x103806f4 bar1
foo2 #10 0x0f6dd704 bar2 foo2#10 0x0f6dd704 bar2
foo3 bar3 foo3 bar3

I have tried the following: 我尝试了以下方法:

^((?!#[\\d]{1,2} 0x[0-9a-f]{8}).)*$ ^((?!#[\\ d] {1,2} 0x [0-9a-f] {8})。)* $

which gets 得到

foo bar 富吧
foo3 bar3 foo3 bar3

and

^((?!#[\\d]{1,2} 0x[0-9a-f]{8}).)* ^((?!#[\\ d] {1,2} 0x [0-9a-f] {8})。)*

which gets 得到

foo bar 富吧
foo1 foo1
foo2 foo2
foo3 bar3 foo3 bar3

But what im trying to get is 但是我想得到的是

foo bar 富吧
foo1 bar1 foo1 bar1
foo2 bar2 foo2 bar2
foo3 bar3 foo3 bar3

How can I achieve this? 我该如何实现?

You need to do replace instead of matching in-order to get the desired output. 你需要做的替代,而不是按顺序以获得所需的输出匹配。

\s*#\d{1,2} 0x[0-9a-f]{8}

Use the above regex and then replace the match with an empty string. 使用上面的正则表达式,然后将匹配项替换为空字符串。

DEMO 演示

If you're wanting the beginning and ending non-whitespace characters, using a Negative Lookahead is not going to do the job. 如果您需要开头和结尾的非空白字符,则使用负超前行将无法完成任务。 You could match your expected output as follows: 您可以匹配预期的输出,如下所示:

^(\S+).*?(\S+)$

Then in your preferred language, you can combine the match results: example ... 然后,可以用您喜欢的语言组合匹配结果: example ...

>>> import re
>>> s = '''foo bar
foo1 #9 0x103806f4 bar1
foo2 #10 0x0f6dd704 bar2
foo3 bar3'''
...
>>> for m in re.finditer(r'(?m)^(\S+).*?(\S+)$', s):
...     print(" ".join(m.groups()))

foo bar
foo1 bar1
foo2 bar2
foo3 bar3

Instead of using regex, consider splitting the string and joining the indexes together. 与其使用正则表达式,不如考虑分割字符串并将索引连接在一起。

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

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