Home > other >  Regex to match path that contains wildcards less than 3 levels deep
Regex to match path that contains wildcards less than 3 levels deep

Time:01-19

I am trying to find a way of matching paths, which contain a wildcard '*' below 3 subdirectories. Just to illustrate some examples:

*              <-- match
abc*/def/ghij  <-- match
/abc/*def/ghij <-- match
/abc/def/*     <-- no match

My goal is to then use this pattern in a SQL statement like REGEXP_LIKE.

I've tried working my way with this regex ^(?:[^/*]*\/)*([^/] )$, but it works in reverse (i.e. not matching when I have wildcards < 3 levels down) and it doesn't seem to consider when the path is only '*' or begins with it.

CodePudding user response:

If you can use negative look-aheads. Assuming you want the whole whole path first you match any path with * with (.*\*.*) (0 or more of any character, a wildcard, then 0 or more of any character). To omit those that are too deep use (?!(\/)[^*/]*\1[^*/]*\1) where (\/) is not repeated 3 times and between the slashes there are no other slashes or wild chars.

^(?!(\/)[^*/]*\1[^*/]*\1)(.*\*.*)
  • Related