Skip to content

Regex to capture the staring with specific word or character and ending with either one of the word

Want to capture the string after the last slash and before either a (; sid=) word or a (?) character.

sample data:

  1. sessionId=30a793b1-ed7e-464a-a630; Url=; sid=KJ4dgQGdhg7dDn1h0TLsqhsdfhsfhjhsdjfhjshdjfhjsfddscg139bjXZQdkbHpzf9l6wy1GdK5XZp; targetUrl=;

  2. sessionId=sfdsdfsd-ba57-4e21-a39f-34; Url=; sid=Q4hWgR1GpQb8xWTLpQB2yyyzmYRgXgFlJLGTc0QJyZbW targetUrl= mybook/order/newbooking/page1?id=123;

  3. sessionId=0e1acab1-45b8-sdf3454fds-afc1-sdf435sdfds; Url=; sid=hkm2gRSL2t5ScKSJKSJn3vg2sfdsfdsfdsfdsfdfdsfdsfdsfvJZkDD3ng0kYTjhNQw8mFZMn; targetUrl=;

Expecting the below output:

 1. itemSummary
 2. itemList
 3. ''(empty string)

Have build the below regex to capture it but its 100% accurate. It is capturing some additional part.


Url=.*/(.*)(; sid|?)

Could you please help me to improve the regex to get desired output?

Thanks in advance!


You may use this regex in Java with a greedy match after Url=:

bUrl=S+/([^?;/]+)(?=; sid|?)

RegEx Demo

RegEx Demo:

  • b: Word boundary
  • Url=: Match text Url=
  • S+/: Match 1+ non-whitespace characters followed by a /
  • ([^?;/]+): Match 1+ of a character that not ? and ; and /
  • (?=; sid|?): Lookahead to assert that we have ; sid or ? ahead