Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

regex: negate a group with condition

is it possible to match strings if a group is not present between a starting and end position, except if the group is followed by a certain character e.g. ‘§’?

# match if '\.\s' is not present between 'start' and 'end'
re.search(r'start((?!\.\s).)*end', string)

for example those two strings should match:

string = 'start abc abc abc.end. '
string = 'start abc abc abc. §end '

but this string shouldn’t match:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

string = 'start abc abc abc. end. '

a solution would be to set a word boundary: start((?!\.\s\b).)*end
but i am specifically looking to set a specific character that may be followed be the negated group

>Solution :

You can add another negative lookahead after \.\s

start((?!\.\s(?!§)).)*end

See this demo at regex101

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading