繁体   English   中英

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

[英]Regular Expression - Parse amount from given string

我有一个字符串(如文本短信),我想从中解析数量。 但它只给出浮动金额。

示例字符串:-

  1. 2020 年 8 月 18 日,您的账户 188383xxxx 存入 3000 卢比。 Total aval bal inr 23044.22 blah blah blah ...

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

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

Output 的表达:-

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

如果它是 integer 数量,我也想为第一个字符串结果获得inr 3000 我错过了什么?

这里最好的方法可能是使用正式的 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);

这打印:

[3000, 23044.22]

使用的正则表达式模式是:

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

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

替代正则表达式:

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

上下文中的正则表达式:

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

暂无
暂无

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

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