简体   繁体   中英

Regex: Everything except some pattern

I have a string:

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

I have tried the following:

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

which gets

foo bar
foo3 bar3

and

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

which gets

foo bar
foo1
foo2
foo3 bar3

But what im trying to get is

foo bar
foo1 bar1
foo2 bar2
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 ...

>>> 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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