开发者

Regex - URL with a specific beginning and end with something in the middle

I'd like to select all strings (parts of URLs) with a "/en" at the beginning, then a random string in the middle and at the end a "aspx", e.g. "/en/about-us-or/any-other-string-12345/page.aspx"

Here's my current approach.

^/en.*aspx$
  • ^/en -- at the beginning a "/en"

  • .开发者_开发问答* -- then a random string in the middle

  • aspx$ -- and a aspx at the end

I wonder why it doesn't work. Any idea?


My guess is that your input strings are complete URIs (like this: "http://www.example.com/en/about-us-or/any-other-string-12345/page.aspx").

If so, your regex will fail because the ^ flag matches the beginning of a string, which in this case is right before the h in http.

The $ character will also cause the regex to fail on URIs that look like this "http://www.example.com/en/about-us-or/any-other-string-12345/page.aspx?parameter=value".

Edit: To actually answer to your question - try dropping the ^ and $ from your regex: /en.*aspx


the / character has a special meaning in regex -- so you will have to escape it (\/) also group the dot-star (.*) :

^\/en(.*)aspx$


My guess is that you are using this as part of a .htaccess rewrite rule. The URL-path presented to RewriteRule is on a per-directory basis and will never have an initial / (it is stripped). Try removing the initial / from your pattern like so:

^en/(.*)\.aspx$
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜