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?

4

1 回答 1

2

只是纠正正则表达式本身

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

问题是使/可选允许正则表达式组合多个 [\p{L}\p{N}-] 组以匹配超过 36 或 51 个字符等等。我做了一个假设,你不能有http://www.mydomain.p.ht//1234561-234561234561234-56123456123456şğ,注意双/

有关更多信息,我强烈推荐http://www.regular-expressions.info/

于 2013-04-11T15:40:53.670 回答