import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "<loc>[^>]*\\K\\d{6}(?=/</loc>)";
final String string = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
+ "<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\n"
+ " <url>\n"
+ " <loc>https://somelink.com/category/building-materials/concrete/hand-tools/</loc>\n"
+ " <lastmod>2022-09-11T02:10:42+02:00</lastmod>\n"
+ " <changefreq>weekly</changefreq>\n"
+ " <priority>0.8</priority>\n"
+ " </url>\n"
+ " <url>\n"
+ " <loc>https://somelink.com/category/building-materials/concrete/hand-tools/screws/screws-145890/</loc>\n"
+ " <lastmod>2022-09-11T02:11:06+02:00</lastmod>\n"
+ " <changefreq>weekly</changefreq>\n"
+ " <priority>0.8</priority>\n"
+ " </url>\n"
+ " <url>\n"
+ " <loc>https://somelink.com/category/building-materials/concrete/hand-tools/screws/screws-145489/</loc>\n"
+ " <lastmod>2022-09-11T02:11:14+02:00</lastmod>\n"
+ " <changefreq>weekly</changefreq>\n"
+ " <priority>0.8</priority>\n"
+ " </url>\n"
+ " <url>\n"
+ " <loc>https://somelink.com/category/building-materials/concrete/hand-tools/hammer/hammer-145488/</loc>\n"
+ " <lastmod>2022-09-11T02:10:42+02:00</lastmod>\n"
+ " <changefreq>weekly</changefreq>\n"
+ " <priority>0.8</priority>\n"
+ " </url>\n"
+ " <url>\n"
+ " <loc>https://somelink.com/category/inside/heating/floor-heating/pert-222-010274/</loc>\n"
+ " <lastmod>2022-09-11T02:11:06+02:00</lastmod>\n"
+ " <changefreq>weekly</changefreq>\n"
+ " <priority>0.8</priority>\n"
+ " </url>\n"
+ " <url>\n"
+ " <loc>https://somelink.com/category/building-materials/paint/</loc>\n"
+ " <lastmod>2022-09-11T02:11:14+02:00</lastmod>\n"
+ " <changefreq>weekly</changefreq>\n"
+ " <priority>0.8</priority>\n"
+ " </url>\n"
+ " <url>\n"
+ " <loc>https://somelink.com/category/building-materials/screws-and-nails/</loc>\n"
+ " <lastmod>2022-09-11T02:10:42+02:00</lastmod>\n"
+ " <changefreq>weekly</changefreq>\n"
+ " <priority>0.8</priority>\n"
+ " </url>\n"
+ " <url>\n"
+ " <loc>https://somelink.com/category/building-materials/concrete/power-toools/</loc>\n"
+ " <lastmod>2022-09-11T02:11:06+02:00</lastmod>\n"
+ " <changefreq>weekly</changefreq>\n"
+ " <priority>0.8</priority>\n"
+ " </url>\n"
+ " <url>\n"
+ " <loc>https://somelink.com/category/inside/heating/floor-heating/pert-182-010272/</loc>\n"
+ " <lastmod>2022-09-11T02:11:14+02:00</lastmod>\n"
+ " <changefreq>weekly</changefreq>\n"
+ " <priority>0.8</priority>\n"
+ " </url>\n"
+ " <url>\n"
+ " <loc>https://somelink.com/category/inside/heating/floor-heating/pert-202-010273/</loc>\n"
+ " <lastmod>2022-09-11T02:10:42+02:00</lastmod>\n"
+ " <changefreq>weekly</changefreq>\n"
+ " <priority>0.8</priority>\n"
+ " </url>\n"
+ " <url>\n"
+ " <loc>https://somelink.com/category/inside/bathroom/</loc>\n"
+ " <lastmod>2022-09-11T02:11:06+02:00</lastmod>\n"
+ " <changefreq>weekly</changefreq>\n"
+ " <priority>0.8</priority>\n"
+ " </url>\n"
+ " <url>\n"
+ " <loc>https://somelink.com/category/inside/pipes/draining-pipes-168544/</loc>\n"
+ " <lastmod>2022-09-11T02:11:14+02:00</lastmod>\n"
+ " <changefreq>weekly</changefreq>\n"
+ " <priority>0.8</priority>\n"
+ " </url>\n"
+ "</xml>";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println("Full match: " + matcher.group(0));
for (int i = 1; i <= matcher.groupCount(); i++) {
System.out.println("Group " + i + ": " + matcher.group(i));
}
}
}
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Java, please visit: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html