Search code examples
regexpreg-matchpcre

PCRE regex lookahead


I can't get PCRE lookahead to work correctly, help will be appreciated, I am finding it difficult to explain but here we go, take this pattern for example:

/^\/page(\/?[a-z0-9\.\_\-]+)*\/?$/i

This matches:

  • /page
  • /page/
  • /page/abc/def/

which is good, but this also matches '/pageabcd' which is supposed to be wrong. So basically for a URI trailing "/" should be optional unless there is some other [a-z0-9\.\-\_]*

I have helplessly tried ^\/page(?=(\/)(\/?[a-z0-9\.\_\-]+)*|(\/)?)$ while I knew I am doing something wrong


Solution

  • You may enforce the presence of a letter, digit, ., _ or - by removing the ? quantifier after / inside the quantified capturing group. Also, you may use \w instead of [a-zA-Z0-9_] to shorten the pattern a bit. A non-capturing group will also be more appropriate since you do not need to capture the value of a repeated capturing group (replace ( with (?:).

    Use

    /^\/page(?:\/[\w.-]+)*\/?$/i
                ^ 
    

    See the regex demo.