I have strings and want to parse.
Examples:
Airtime payment made for UGX5,000 to MTN 077xxxx39
You have received UGX200,000 from 077xxx4515
You have sent UGX1,033,000 to 078xxx3370
In the above case, i want to parse the money string and the telephone number string. I thought of using split, but the strings not the same numbers.
Another type that I want to parse.
Your balance is UGX200,196
Here can I use split and get the last token....
But for the first category, I think that need regex....and I'm not competent with it!
Any one to help me out?
Ronald
This should work:
String[] str = new String[]{"Airtime payment made for UGX5,000 to MTN 07745454539", "You have received UGX200,000 from 07744434515", "You have sent UGX1,033,000 to 0744343370", "Your balance is UGX200,196"};
Pattern p = Pattern.compile("(UGX[0-9,]+)([A-Za-z ]+(\\d+))?");
for(String s : str)
{
Matcher m = p.matcher(s);
if(m.find())
{
if(m.group(3) != null)
{
System.out.println("Payment: " + m.group(1));
System.out.println("Telephone number: " + m.group(3));
}
else if(m.group(1) != null)
{
System.out.println("Balance: " + m.group(1));
}
}
}
Yields:
Payment: UGX5,000
Telephone number: 07745454539
Payment: UGX200,000
Telephone number: 07744434515
Payment: UGX1,033,000
Telephone number: 0744343370
Balance: UGX200,196
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments