regex: negate a group with condition

Question:

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:

string = 'start abc abc abc. end. '

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

Asked By: Blob

||

Answers:

You can add another negative lookahead after .s

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

See this demo at regex101

Answered By: bobble bubble
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.