Panama Jack
Panama Jack

Reputation: 24458

Regex - Exclude URLs by keywords

I am trying to use StackPath's EdgeRules and their documentation is not very clear or good. I need to match urls in multiple directories but exclude any URL's that have the extension m3u8 in it or the word segment in it. This is their docs EdgeRules

This works to limit it to 2 directories.

/(https://example.com(/(pics|vids)/).*)/

But then this doesn't work.

/(https://example.com(/(pix|vids)/).+(?!m3u8|segment).*)/

I've been trying to use https://regex101.com/ but nothing I try seems to work. I don't even know what kind of regex they use. Hopefully can get some help with this.

Upvotes: 0

Views: 357

Answers (2)

Peter Thoeny
Peter Thoeny

Reputation: 7616

The EdgeRules docs do not mention the regex flavor they support, and from the examples it is not clear. Also the example /(^http://example.com(/.*/)+.$)/ shows non-escaped backslashes, indicating this is non-standard regex.

I see no other way than using a negative lookahead to exclude arbitrary patterns. Assuming their regex does support it you can try:

/^https://example.com/(pix|vids)/(?!.*\bm3u8\b)(?!.*\bsegment\b).*$/

Or with properly escaped special chars:

/^https:\/\/example\.com/(pix|vids)/(?!.*\bm3u8\b)(?!.*\bsegment\b).*$/

Explanation of regex:

  • ^ -- anchor at start of string
  • https:\/\/example\.com/ -- literal https://example.com/
  • (pix|vids) -- literal pix or vids
  • / -- slash
  • (?!.*\bm3u8\b) -- negative lookahead for m3u8, anchored on both sides with \b
  • (?!.*\bsegment\b) -- ditto for segment
  • .*$ -- any other chars up to end of string

Upvotes: 0

BrendanOtherwhyz
BrendanOtherwhyz

Reputation: 481

I can't test this so apologies if its something else wrong...

The negative look aheads need to be side by side, not wrapped in parentheses separated by or (|). I also added a end of line character ($) at the end of .m3u8.

(https://example.com(/(pix|vids)/)(?!.*\.m3u8$)(?!.*segment.*).*)

See this example: https://regex101.com/r/reVHWt/1

Upvotes: 1

Related Questions