简体   繁体   中英

Regular Expression - Parse amount from given string

I have a string (like text sms) from which I want to parse amount. But it only gives float amount.

Example strings:-

  1. Your account 188383xxxx is credited with inr 3000 on 18aug20. Total aval bal inr 23044.22 blah blah blah...

  2. Your account 188383xxxx is credited with inr 3000.33 on 18aug20. Total aval bal inr 23044.22 blah blah blah..."

Regular Expression that I am using:- "(inr)+[\s]?+[0-9] +[\,] +[0-9] +[\.] [0-9]{2}"

Output of expression:-

String 1) = inr 23044.22 String 2) = inr 3000.33 , inr 23044.22

I want get inr 3000 for first string result also if it is integer amount. What I am missing?

The best approach here would probably to use a formal Java regex pattern matcher, and iterate over the input string to find all integer/floating amounts:

String input = "Your account 188383xxxx is credited with inr 3000 on 18aug20. Total aval bal inr 23044.22 blah blah blah...";
String pattern = "\\binr\\s+(\\d+(?:\\.\\d+)?)\\b";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(input);
List<String> amounts = new ArrayList<>();
while (m.find()) {
    amounts.add(m.group(1));
}

System.out.println(amounts);

This prints:

[3000, 23044.22]

The regex pattern used was:

\binr\s+(\d+(?:\.\d+)?)\b

This says to match either an integer, or an integer which is followed by a decimal component (ie a float). We also preface the pattern with inr , to make sure we are matching a Rupee amount, and not some other number (eg not the account number).

Alternative regex:

"\\binr\\s+([\\d\\.]+)"

Regex in context:

public static void main(String[] args) {
   String input = "Your account 188383xxxx is credited with bbbbinr 30022 inr 3000 on 18aug20."
           + " Total aval bal inr 23044.22 blah blah blah. In bookkeeping, an account refers to assets, \n"
           + "liabilities, income, expenses, and equity, as represented by individual\n"
           + "ledger pages, to which changes in value are chronologically recorded with\n"
           + " debit and credit entries. These entries, referred to as postings, \n"
           + "become part of a book of final entry or ledger. Examples of common financial\n"
           + " accounts are sales, accounts [1]receivable, mortgages, loans, PP&E, common \n"
           + "stock, sales, services, wages and payroll.\n"
           + "Your account 188383xxxx is credited with inr 3000.33 on 18aug20. Total aval bal"
           + " inr 23044.22 blah blah blah...";

    Matcher matcher = Pattern.compile("\\binr\\s+([\\d\\.]+)").matcher(input);

    while(matcher.find()) {
        String amount = matcher.group(1);
        System.out.println(amount); // Output is here :)
    }
}

Output:

3000
23044.22
3000.33
23044.22

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM