【问题标题】:how to get the balance or available amount from SMS Regex如何从 SMS Regex 获取余额或可用金额
【发布时间】:2021-11-05 20:30:41
【问题描述】:

我想从短信中提取余额

我的短信内容是

account ending with ********9415 has been credited with Rs. 5000. Updated account balance is Rs. 13086.18

Your card transaction of Rs.417 is successful. Your updated credit balance is Rs.78,468

Dear Cardmember, payment of Rs.7657.00 has been received towards your Bank Credit Card ending with 3459 on 12-11-2020 through NEFT. Payment is subject to realisation. Your available Credit limit now is Rs. 173281.31.

这是我目前的代码

(?i)(?:\sbalance\s*)([A-Za-z0-9]+\s[A-Za-z0-9]+)
(?i)(?:\scredit limit\s*)([A-Za-z0-9]+\s[A-Za-z0-9]+)

那么如何从上面的短信中获取金额呢?

【问题讨论】:

标签: java regex


【解决方案1】:

你可以使用

(?i)\b(?:balance|credit\s+limit)\D+(\d+(?:[.,]\d+)?)

请参阅regex demo详情

  • (?i) - 不区分大小写的嵌入式标志选项
  • \b - 单词边界
  • (?:balance|credit\s+limit) - balancecredit limit 之间有一个或多个空格
  • \D+ - 一个或多个非数字字符
  • (\d+(?:[.,]\d+)?) - 第 1 组(您需要抓取的值):一位或多位数字,然后是可选的 ., 序列和一位或多位数字。如果可以有多个点/逗号,请将 ? 替换为 *

Java demo

String regex = "(?i)\\b(?:balance|credit\\s+limit)\\D+(\\d+(?:[.,]\\d+)?)";
String text = "account ending with ********9415 has been credited with Rs. 5000. Updated account balance is Rs. 13086.18\n\nYour card transaction of Rs.417 is successful. Your updated credit balance is Rs.78,468\n\nDear Cardmember, payment of Rs.7657.00 has been received towards your Bank Credit Card ending with 3459 on 12-11-2020 through NEFT. Payment is subject to realisation. Your available Credit limit now is Rs. 173281.31.";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(text);
while (matcher.find()){
    System.out.println(matcher.group(1)); 
} 

输出:

13086.18
78,468
173281.31

【讨论】:

  • 我在这里投票,\D+ 也有效
【解决方案2】:

如果您的数据始终具有该格式,您可以使用捕获组并匹配 Rs。来自示例数据:后跟字符 a-zA-Z 和空格:

(?i)\b(?:balance|credit\s+limit)\s+[A-Za-z]+(?:\s+[A-Za-z]+)*\s+Rs\.\s*(\d+(?:[.,]\d+)*)
  • (?i) 不区分大小写匹配的行内修饰符
  • \b(?:balance|credit\s+limit) 匹配 2 个备选方案之一
  • \s+[A-Za-z]+ 匹配 1+ 个空格字符和 1+ 个字符 A-Za-z
  • (?:\s+[A-Za-z]+)* 可以选择重复前面的模式,前面有 1 个以上的空白字符
  • \s+Rs\.\s* 匹配 1+ whitspace 字符和 Rs.
  • (\d+(?:[.,]\d+)*) 捕获 组 1 中的 1+ 位数字,可选地重复 ., 和 1+ 位数字

查看regex demo | Java demo.

String regex = "(?i)\\b(?:balance|credit\\s+limit)\\s+[A-Za-z]+(?:\\s+[A-Za-z]+)*\\s+Rs\\.\\s*(\\d+(?:[.,]\\d+)*)";
String string = "account ending with ********9415 has been credited with Rs. 5000. Updated account balance is Rs. 13086.18\n\n"
+ "Your card transaction of Rs.417 is successful. Your updated credit balance is Rs.78,468\n\n"
+ "Dear Cardmember, payment of Rs.7657.00 has been received towards your Bank Credit Card ending with 3459 on 12-11-2020 through NEFT. Payment is subject to realisation. Your available Credit limit now is Rs. 173281.31.";

Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(string);

while (matcher.find()) {
    System.out.println(matcher.group(1));
}

输出

13086.18
78,468
173281.31

【讨论】:

    【解决方案3】:

    最好避免在这里编写复杂的正则表达式。它很容易出错,这样的错误很难调试。

    我们可以通过一系列字符串拆分来简化问题并将其转化为一系列较小的子问题。

    1. 首先替换“卢比”。带有“XXX”的子字符串。我们需要这样做以删除Rs. 中的点,以便以后. 可以用作拆分令牌。
    2. 在“.”上分割字符串现在符号并将其保存到数组'sen​​tences'
    3. 对于sentences 数组中的每个字符串,只选择“XXX”部分之后的所有内容。这将为您提供价格字符串“32.12”
    4. 现在您只需使用 Double.valueOf() 将其转换为数字

    这样的代码比使用正则表达式的代码更容易维护和理解。

    【讨论】:

      猜你喜欢
      • 2022-09-25
      • 2016-12-28
      • 1970-01-01
      • 1970-01-01
      • 2016-03-17
      • 2021-12-08
      • 1970-01-01
      • 1970-01-01
      • 2016-03-26
      相关资源
      最近更新 更多