[英]Regex: Everything except some pattern
I have a string: 我有一个字符串:
foo bar
富吧
foo1 #9 0x103806f4 bar1foo1#9 0x103806f4 bar1
foo2 #10 0x0f6dd704 bar2foo2#10 0x0f6dd704 bar2
foo3 bar3foo3 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 bar3foo3 bar3
and 和
^((?!#[\\d]{1,2} 0x[0-9a-f]{8}).)*
^((?!#[\\ d] {1,2} 0x [0-9a-f] {8})。)*
which gets 得到
foo bar
富吧
foo1foo1
foo2foo2
foo3 bar3foo3 bar3
But what im trying to get is 但是我想得到的是
foo bar
富吧
foo1 bar1foo1 bar1
foo2 bar2foo2 bar2
foo3 bar3foo3 bar3
How can I achieve this? 我该如何实现?
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: python example ... 然后,可以用您喜欢的语言组合匹配结果: python 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.