1

I search a regex in PHP to match a simple URL path with specific characters and not more.

My regex don't work exactly (flag 'gm' only for test. in working process please without 'g' for more exactly.):

/^\/[A-Za-z0-9-]+\/?[A-Za-z0-9-]+\/?[A-Za-z0-9-]+\/?[A-Za-z0-9-]+\/?$/gm

URL path Examples with comment:

#match: YES
/
/trip-001
/trip-001/
/trip-001/summer-2019
/trip-001/summer-2019/
/trip-001/summer-2019/ibiza-001/
/trip-001/summer-2019/ibiza-001/PICT-001

#match: NO
//
trip-001
trip-001/
trip-001/summer-2019
trip-001/summer-2019/
trip-001/summer-2019/ibiza-001/
trip-001/summer-2019/ibiza-001/PICT-001

//trip-001
trip-001//
//trip-001/summer-2019
//trip-001//summer-2019
trip-001//summer-2019
//trip-001/summer-2019/
//trip-001//summer-2019//
trip-001//summer-2019/
trip-001/summer-2019//
trip-001/summer-2019/
trip-001/summer-2019/ibiza-001/
//trip-001/summer-2019/ibiza-001/
//trip-001//summer-2019/ibiza-001/
//trip-001/summer-2019//ibiza-001/
//trip-001/summer-2019/ibiza-001//
trip-001/summer-2019/ibiza-001//
trip-001/summer-2019/ibiza-001/
trip-001/summer-2019/ibiza-001/PICT-001
//trip-001/summer-2019/ibiza-001/PICT-001
# and similar

/trip-001/summer-2019/ibiza-001/PICT-001/
/trip-001/summer-2019/ibiza-001/whatever-987/PICT001
/trip-001/summer-2019/ibiza-001/whatever-987/PICT001/

trip-001/summer-2019/ibiza-001/PICT-001/
trip-001/summer-2019/ibiza-001/whatever-987/PICT001
trip-001/summer-2019/ibiza-001/whatever-987/PICT001/

I have no idea it works with {n}.

Only this charset: A-Z a-z 0-9 - / and exactly no more. Please no \d for digits.

It's for a !preg_match() in PHP.

EDIT: Leading slash is a must have. Double slash and more is not allowed. Trailing slash yes or no.

0

1 Answer 1

1

It appears the URL should only be valid if there are not more than 5 slashes.

You may adjust your pattern as

^(?!(?:[^\/]*\/){5})(?:(?:\/[A-Za-z0-9-]+){1,4}\/?|\/)$

See regex demo

Details

  • ^ - start of string
  • (?!(?:[^\/]*\/){5}) - a negative lookahead that fails the match if there are 5 occurrences of / chars in the string
  • (?: - start of the non-capturing group:
    • (?:\/[A-Za-z0-9-]+){1,4}\/? - 1 to 4 occurrences of a / and 1+ ASCII alphanumeric or - chars and then an optional / char
    • | - or
    • \/ - a single / char in the string
  • ) - end of the non-capturing group
  • $ - end of string.
Sign up to request clarification or add additional context in comments.

11 Comments

@Malama Correct. That is what I suggest, too. See updated samples demo.
@Malama (?:[^\/]*\/){5} matches 5 occurrences of any 0+ chars other than / followed with /. It is a pattern inside a negative lookahead, it just fails any string that has 5 or more / chars in it. You do not need to replace it with any pattern, the consuming pattern will validate the string. (?:...) is a non-capturing group.
@Malama Both my regexps require / as the first char. See your updated sample demo. If you need more clarifications please ask here, add more comments.
@Malama Is empty string allowed?
@Malama I fixed the pattern in the answer.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.