Sacha Vorbeck
Sacha Vorbeck

Reputation: 327

Regex for excluding several strings

I need a regex that excludes lines with given strings from matching. Here`s what I found out so far:

https://regexr.com/42j9r

Regex:

(?<!\/)\shttps

Test-Text:

string.pdf https
string.html https
stringEndingWithSlash/ https
stringEndingWithoutSlash https

This only excludes the 3rd line. But I need the expression to match only the lines with strings not ending with .pdf, .html or a slash. So only the last line of the example should match. How can I use the logical | or here? Or is there another solution? Thank you - all the best, Sacha

Upvotes: 0

Views: 57

Answers (3)

trincot
trincot

Reputation: 350242

I realise an answer has been accepted, but it will be more efficient to not do a look ahead at each individual character, but do two look-behinds where and when they are needed only:

^.*[^\/] https$(?<!\.pdf https)(?<!\.html https)

... using the gm flags.

Upvotes: 1

Alfredo A.
Alfredo A.

Reputation: 1777

You could try this

^((?!pdf|html|\/).)*\shttps$

Try it here

Upvotes: 1

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521178

The following pattern seems to be working:

^\w+(?!\.pdf|\.html|\/) https$

Demo

This matches some initial word, which does not end in either .pdf, .html, or a forward slash, which is then followed by a space and https. It uses a negative lookahead to do this. While there might be solutions that do not use lookarounds, they seem to be easiest way to implement the logic you want.

Upvotes: 1

Related Questions