Regex to match URL path segments not containing a symbol

Dmitry Gusev :

Given URL that may contain zero or many "ID-segments" (a segment that contains _ character), i.e.:

/v1/customer/cus_id/cards/card_id

what would be a regex that can find & replace all these segments with predefined string ([^/]+ in my case), so that end result would look like this:

/v1/customer/[^/]+/cards/[^/]+
Roronoa_D._Law :

This could work [^/]+_[^/]+ Well at least it worked with your example. What I did :

String x = "/v1/customer/cus_id/cards/card_id";
System.out.println(x.replaceAll("[^/]+_[^/]+","[^/]+"));

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=328942&siteId=1