I made a project myself where I need to extract a certain string of a URL. Before I used
Arrays.asList(URL.split("/")).get(6);
but with this I only can get it, if it is after the 6th /. I knew there is something with matcher() but I don't get it to work.
For example:
https://www.amazon.de/gp/product/SOMETEXTHERE/ref=oh_aui_detailpage_o00_s00?ie=UTF8&psc=1
I want to extract out the SOMETEXTHERE. But this characters are changing so I can't just say that it is always the same. And I need the SOMETEXT then in a seperate string.
Use group(1)
to retrive the text between product/
and /ref
:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile(".*product/(.*)/ref.*");
Matcher matcher = pattern.matcher("https://www.amazon.de/gp/product/SOMETEXTHERE/ref=oh_aui_detailpage_o00_s00?ie=UTF8&psc=1");
if (matcher.matches()) {
System.out.println(matcher.group(1));
}
}
}