3

I want a regex that will always return the last part of an url before the query string parameters and without the jessionid if present.

Here's some url examples:

http://www.somesite.com/some/path/test.action;jsessionid=000063vCmvJAn7VWyymA_dPsHZs:16u9pglit?sort=2&param1=1&param2=2
http://www.somesite.com/some/path/test;jsessionid=000063vCmvJAn7VWyymA_dPsHZs:16u9pglit?sort=2&param1=1&param2=2
http://www.somesite.com/some/path/test.action?sort=2&param1=1&param2=2
http://www.somesite.com/some/path/test?sort=2&param1=1&param2=2

Here's my regex so far:

.*http://.*/some/path.*/(.*);?.*\?.*

It is working for the url that does not contain jsessionid, but will return test;jessionid=... if it is present.

To test: http://regex101.com/r/fM0mE2

4

1 回答 1

2

我会使用这个正则表达式

.*http:\/\/.*\/some\/path.*\/([^;\?]+);?.*\?.*
                              ^^^^^^ 

基本上匹配任何不是;或的东西?。我认为它可能会缩短为:

.*http:\/\/.*\/some\/path.*\/([^;\?]+)
于 2013-05-24T19:09:02.110 回答