1

I am trying to extract a the substring between the second slash and the first = character. Currently I'm using split to achieve this. How can I use regex to do this?. Thanks in advance.

input - "/test/example=1244 output- example

This is what I'm using currently:

String test= inputString.split("/")[2].split("=")[0];
wazza
  • 173
  • 5
  • 15
  • 1
    Looks like you are looking to create a regex, but do not know where to get started. Please check [Reference - What does this regex mean](https://stackoverflow.com/questions/22937618) resource, it has plenty of hints. Also, refer to [Learning Regular Expressions](https://stackoverflow.com/questions/4736) post for some basic regex info. Once you get some expression ready and still have issues with the solution, please edit the question with the latest details and we'll be glad to help you fix the problem. – Wiktor Stribiżew Sep 21 '20 at 16:18
  • why not use something that is already built in? https://stackoverflow.com/questions/21545912/url-parsing-in-java – RisingSun Sep 21 '20 at 16:18

1 Answers1

1

You could split on a regex alternation of slash or equals:

String test = "/test/example=1244";
String output = test.split("[/=]")[2];
System.out.println(output);

This prints:

example
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360