简体   繁体   English

正则表达式 - 从给定字符串中解析数量

[英]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. 2020 年 8 月 18 日,您的账户 188383xxxx 存入 3000 卢比。 Total aval bal inr 23044.22 blah blah blah... Total aval bal inr 23044.22 blah blah blah ...

  2. Your account 188383xxxx is credited with inr 3000.33 on 18aug20. 2020 年 8 月 18 日,您的账户 188383xxxx 被记入 3000.33 卢比。 Total aval bal inr 23044.22 blah blah blah..." Total aval bal inr 23044.22 blah blah blah..."

Regular Expression that I am using:- "(inr)+[\s]?+[0-9] +[\,] +[0-9] +[\.] [0-9]{2}"我正在使用的正则表达式:- "(inr)+[\s]?+[0-9] +[\,] +[0-9] +[\.] [0-9]{2}"

Output of expression:- Output 的表达:-

String 1) = inr 23044.22 String 2) = inr 3000.33 , inr 23044.22字符串 1) = inr 23044.22字符串 2) = inr 3000.33 , inr 23044.22

I want get inr 3000 for first string result also if it is integer amount.如果它是 integer 数量,我也想为第一个字符串结果获得inr 3000 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:这里最好的方法可能是使用正式的 Java 正则表达式模式匹配器,并遍历输入字符串以查找所有整数/浮点数:

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).这表示匹配 integer 或 integer,后跟一个小数部分(即浮点数)。 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).我们还在模式前加上inr ,以确保我们匹配的是卢比金额,而不是其他数字(例如,不是帐号)。

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: Output:

3000
23044.22
3000.33
23044.22

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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