Skip to content
Advertisement

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=https://www.example.com/mybook/order/newbooking/itemSummary; sid=KJ4dgQGdhg7dDn1h0TLsqhsdfhsfhjhsdjfhjshdjfhjsfddscg139bjXZQdkbHpzf9l6wy1GdK5XZp; targetUrl=https://www.example.com/mybook/order/newbooking/page1?id=122;

  2. sessionId=sfdsdfsd-ba57-4e21-a39f-34; Url=https://www.example.com/mybook/order/newbooking/itemList?id=76734&para=jhjdfhj&type=new&ordertype=kjkf&memberid=273647632&iSearch=true; sid=Q4hWgR1GpQb8xWTLpQB2yyyzmYRgXgFlJLGTc0QJyZbW targetUrl=https://www.example.com/ mybook/order/newbooking/page1?id=123;

  3. sessionId=0e1acab1-45b8-sdf3454fds-afc1-sdf435sdfds; Url=https://www.example.com/mybook/order/newbooking/; sid=hkm2gRSL2t5ScKSJKSJn3vg2sfdsfdsfdsfdsfdfdsfdsfdsfvJZkDD3ng0kYTjhNQw8mFZMn; targetUrl=https://www.example.com/mybook/order/newbooking/page1?id=343;

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.

Regex

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

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

Thanks in advance!

Advertisement

Answer

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
Advertisement