I have a string
like bellow :
dfdfm;lg 2500$ jshfsnefsfz5405€mnvkjdf64rfmkd554668¢ odsfrknegj 885486¥ dsflkef 588525dollar
I am getting bellow values with this [\\d,]+\\s*\\$|[\\d,]+\\s*€|[\\d,]+\\s*¥|[\\d,]+\\s*¢|[\\d,]+\\s*dollar
:
2500$ 5405€ 554668¢ 885486¥ 588525dollar
Problem : But I don't need to these $ € ¢ ¥ dollar
. How I can delete these in top regex ?
Here is my method :
private String getPrice(String caption) {
String pricePattern = "[\\d,]+\\s*\\$|[\\d,]+\\s*€|[\\d,]+\\s*¥|[\\d,]+\\s*¢|[\\d,]+\\s*dollar|[\\d,]+\\s*Euro";
List<String> lstPrice = new ArrayList<>();
Pattern rPrice = Pattern.compile(pricePattern);
Matcher mPrice = rPrice.matcher(caption);
while (mPrice.find()) {
lstPrice.add(mPrice.group());
}
if (lstPrice.size() > 0) {
return lstPrice.get(0);
}
return "";
}
If you need to return all prices, make sure your getPrice
method returns List<String>
and adjust the regex to match the prices but capture the numbers only:
private List<String> getPrice(String caption) {
String pricePattern = "(?i)(\\d[\\d,]*)\\s*(?:[$€¥¢]|dollar|Euro)";
List<String> lstPrice = new ArrayList<>();
Pattern rPrice = Pattern.compile(pricePattern);
Matcher mPrice = rPrice.matcher(caption);
while (mPrice.find()) {
lstPrice.add(mPrice.group(1));
}
return lstPrice;
}
See the Java demo online.
String s = "dfdfm;lg 2500$ jshfsnefsfz5405€mnvkjdf64rfmkd554668¢ odsfrknegj 885486¥ dsflkef 588525dollar";
System.out.println(getPrice(s));
returns
[2500, 5405, 554668, 885486, 588525]
Pattern details:
(?i)
- a case insensitive modifier (embedded flag option)(\\d[\\d,]*)
- Group 1 capturing a digit and then 0+ digits or ,
\\s*
- 0+ whitespaces(?:[$€¥¢]|dollar|Euro)
- either $
, €
, ¥
, ¢
, dollar
or euro
(case insensitive search is enabled via (?i)
)