Andre Chenier
Andre Chenier

Reputation: 1186

php REGEX to match custom url structure issue

I tried the pattern below with a lot of variations of ( ) and [ ] but I couldn't achieved my aim.

$pattern = '/^http:\/\/www.mydomain.p.ht\/?[\p{L}\p{N}\-]{0,36}\/?[\p{L}\p{N}\-]{0,51}\/?[\p{L}\p{N}\-]{0,101}$/';

My aim is: to match my needed url structure

  1. http://www.mydomain.p.ht
  2. 0 or 1 / slash character
  3. utf-8 aware letters, numbers and dash character. Total length is min 0, max 36
  4. 0 or 1 / slash character
  5. utf-8 aware letters, numbers and dash character. Total length is min 0, max 51
  6. 0 or 1 / slash character
  7. utf-8 aware letters, numbers and dash character. Total length is min 0, max 101. there mustn't exist / slash character at the very end.

currently http://www.mydomain.p.ht/1234561-234561234561234-56123456123456şğ matches but it shouldn't since 1234561-234561234561234-56123456123456şğ part has more than 36 characters.

Can you please correct my pattern?

Upvotes: 1

Views: 244

Answers (1)

Andrew Leap
Andrew Leap

Reputation: 956

Just correcting the regex itself

/^http:\/\/www.mydomain.p.ht(\/|\/[\p{L}\p{N}\-]{1,36}(\/|\/[\p{L}\p{N}\-]{1,51}(\/|\/[\p{L}\p{N}\-]{1,101})?)?)?$/

the issue is that making the / optional allows the regex to combine multiple of the [\p{L}\p{N}-] groups to match more then 36 or 51 chars and so forth. I made one assumption, that you can't have http://www.mydomain.p.ht//1234561-234561234561234-56123456123456şğ, note the double /

For additional info, I highly reccommend http://www.regular-expressions.info/

Upvotes: 2

Related Questions