Skip to content

Commit 10b70a5

Browse files
author
Rai
committed
regex example to find image path and append additional url is added
1 parent d7a41fa commit 10b70a5

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

src/main/java/com/eprogrammerz/examples/java8/general/text_processing/TextProcessing.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,28 @@ public class TextProcessing {
2424
productLinks.put("image2", "/path/to/image1.jpg");
2525
productLinks.put("image3", "/path/to/image2.jpg");
2626
productLinks.put("image4", "/path/to/image3.jpg");
27+
productLinks.put("image5", "/path/to/image3.png");
2728
Product product = new Product("NewProduct", productLinks);
2829
brandAndProduct.setBrand(brand);
2930
brandAndProduct.setProducts(Arrays.asList(product));
3031
}
3132

3233
public static void main(String[] args) {
34+
//to verify String is immutable
35+
// String string = "Delta offered solution, not obstruction, for Qatar inaugural gate access";
36+
// String newString = string.replace("access", "great access");
37+
// System.out.println(string);
38+
// System.out.println(newString);
3339

40+
//need to find the texts with image urls ex. /path/to/image.jpg, /path/to/image1.jpg etc
3441
System.out.println(brandAndProduct);
35-
36-
//need to find the texts with image urls ex. /path/to/image.jpg, /path/to/image1.jpg etc
37-
// Pattern pattern = Pattern.compile("(/.*\\.(?:png|jpg))", CASE_INSENSITIVE);
38-
Pattern pattern = Pattern.compile("(?:/[^/#?]+)+\\.(?:jpg|gif|png)", CASE_INSENSITIVE);
39-
Matcher matcher = pattern.matcher(brandAndProduct.toString());
40-
if(matcher.find()){
41-
System.out.println("Matched....");
42-
System.out.println(matcher.group());
42+
String brandAndProductString = brandAndProduct.toString();
43+
Pattern pattern = Pattern.compile("([^=.*]+\\.(?:jpg|png))", CASE_INSENSITIVE);
44+
Matcher matcher = pattern.matcher(brandAndProductString);
45+
String result = null;
46+
while (matcher.find()){
47+
result = brandAndProductString.replaceAll("([^=.*]+\\.(?:jpg|png))", "delta.com"+matcher.group());
4348
}
49+
System.out.println(result);
4450
}
4551
}

0 commit comments

Comments
 (0)