1

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?

1 Answer 1

2

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/

Sign up to request clarification or add additional context in comments.

1 Comment

thank you so much. Golden shot. +1 for extra explanation. will be accepted after 5min.

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.