简体   繁体   中英

Regex to get uri stem (path) from uri with optional end character

This should be simple but I can't seem to get it exactly right. I need to extract the uri stem/path only from a uri where it could end with a '/' or not and have a query string or not.

So use cases are as follows

  • /shopping/cart ----> /shopping/cart
  • /shopping/cart/ ----> /shopping/cart
  • /shopping/cart/?param1=val1&param2=val2 ----> /shopping/cart
  • /shopping/cart?param1=val1&param2=val2 ----> /shopping/cart

So far I have tried ^(\\/.\\*)\\/ and ^(\\/.\\*)\\/?. I also would prefer to stay away from double groupings if at all possible

使用正向超前断言( 更多信息 )来查找问号:

^(/.*)(?=\?)

Use this Regex

(^\/.*)\/\?

For More reference .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM