Skip to content

Instantly share code, notes, and snippets.

@sharno
Last active August 29, 2015 14:13
Show Gist options
  • Save sharno/c70cbf06377b36168e7f to your computer and use it in GitHub Desktop.
Save sharno/c70cbf06377b36168e7f to your computer and use it in GitHub Desktop.
small currency detector
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
String message= "From HSBC: 08JAN15 METRO MARKET EGP 48.70-";
Matcher currencyMatcher = Pattern.compile("(?:EGP|USD|EUR)(?=\\s\\d*)").matcher(message);
Matcher amountMatcher = Pattern.compile("\\d*,?\\d*,?\\d+\\.\\d+").matcher(message);
Matcher signMatcher = Pattern.compile("[+-]$").matcher(message);
Matcher dateMatcher = Pattern.compile("\\d{2}(JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)\\d{2}").matcher(message);
if (currencyMatcher.find()) {
System.out.println(currencyMatcher.group());
}
if (amountMatcher.find()) {
System.out.println(amountMatcher.group());
}
if (signMatcher.find()) {
System.out.println(signMatcher.group());
}
if (dateMatcher.find()) {
System.out.println(dateMatcher.group());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment