Parsing string below with regex

user3079559

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

npinti

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Regex - parsing string into groups

From Dev

Parsing this string with regex PHP

From Dev

JSON parsing vs Regex based String parsing

From Dev

Java quoted string parsing with regex

From Dev

Regex for parsing sub string of URL

From Dev

regex validation not parsing string in if PHP

From Dev

Parsing CGI Query String Regex

From Dev

Preparing regex for parsing simple string

From Dev

Regex for parsing sub string of URL

From Dev

scala - string parsing without Regex

From Dev

regex parsing a string to extract numbers in shell script

From Dev

Parsing building numbers from a string with regex

From Dev

Why isn't this regex parsing the whole string?

From Dev

C# Regex syntax help for parsing string

From Dev

Parsing string to array using Regex in c#

From Dev

Regex exercise, parsing an accidentally messed up string

From Dev

Can't find a proper regex for parsing the string

From Dev

RegEX : Parsing text for exact string match or same string with underscore at the end

From Dev

Flex regex.replace whole string before parsing starts

From Dev

python regex parsing data from extremely complicated ingredients string

From Dev

Regex from a html parsing, how do I grab a specific string?

From Dev

Java: enrich xml with elements through regex string parsing

From Dev

Json deserializer OR Regex Or Json parsing to convert a Json string in c#

From Dev

How do we replace below 'x' sub string with 'y' sub string using regex in Java?

From Dev

Regex Number Below 90

From Dev

Parsing a string?

From Dev

Parsing text to object with regex

From Dev

Parsing Regex in C

From Dev

Regex parsing citation issue