For a project I set myself I have to filter a 10 character string out of a URL.
For example:
https://www.amazon.de/gp/product/SOMETEXTHERE/ref=oh_aui_detailpage_o00_s00?ie=UTF8&psc=1
I want to filter out the SOMETEXTHERE . But this characters are changing so I can't just say that it is always the same.
Are there any existing functions or so?
You can use regex for this task:
final String string = "https://www.amazon.de/gp/product/B001VGASYA/ref=oh_aui_detailpage_o00_s00?ie=UTF8&psc=1";
final String replacement = String.format("$1%s$3", "REPLACED");
final String result = string.replaceFirst("(.*product\\/)([^\\/]+)(.*)", replacement); // or .replaceAll()
System.out.println(result);
Breakdown:
(.*product\\\\/)([^\\\\/]+)(.*)
(.*product\\\\/)
.*
matches any character (except for line terminators) *
Quantifier — Matches between zero and unlimited times, as many times as possible, giving back as needed (greedy) product matches the characters product literally (case sensitive) \\/
matches the character /
literally (case sensitive) ([^\\\\/]+)
[^\\/]+
+
Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy) \\/
matches the character /
literally (case sensitive) (.*)
.*
matches any character (except for line terminators) *
Quantifier — Matches between zero and unlimited times, as many times as possible, giving back as needed (greedy) If are you sure that only the text that you mentioned will change you can split the string by "/" character then just take the right string from list.
String URL = "https://www.amazon.de/gp/product/B001VGASYA/ref=oh_aui_detailpage_o00_s00?ie=UTF8&psc=1";
String targetString = Arrays.asList(URL.split("/")).get(5);
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.