Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question

1 Answer

up vote 2 down vote accepted

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/

share|improve this answer
thank you so much. Golden shot. +1 for extra explanation. will be accepted after 5min. – Andre Chenier Apr 11 at 15:44
1  
lol, np, I love using regex's – Andrew Leap Apr 11 at 15:45

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.