首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从字符串- java中提取信息

从字符串- java中提取信息
EN

Stack Overflow用户
提问于 2021-06-12 07:05:12
回答 1查看 56关注 0票数 0

我已经创建了一个应用程序来监控收到的短信使用广播接收器。该应用程序监视来自特定发送方的特定消息。我试图从具有以下格式的信息中添加某些信息:

PF56S55yy Confirmed.You已收到Guaranty Bank Limited 910201 on 5/6/21 at 10:07 PM New M is Ksh10,103.45的Ksh6,495.00。通过*377#上的虚拟文本将个人和业务资金分开。

如上文所示,如果以粗体格式显示,我需要的信息,如

代码:收到PF56S55yy金额: 6,495.00来自:担保信托银行有限公司910201日期: 5/6/21时间: 10:07

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-06-12 07:38:38

使用regex,您可以提取所需信息。

https://regex101.com/r/ifuwVg/1上尝试regex

Java代码:

代码语言:javascript
复制
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Example {
    public static void main(String[] args) {
        final String regex = "^([a-zA-Z0-9]+)\\s{1}[a-zA-Z0-9\\.\\s]+Ksh([0-9,.]+)\\sfrom\\s([a-zA-Z0-9\\.\\s]+)\\son\\s([0-9/]+)\\sat\\s([0-9:]+)\\s[A|P]M\\s.*$";
        final String string = "PF56S55yy Confirmed.You have received Ksh6,495.00 from Guaranty Trust Bank Limited 910201 on 5/6/21 at 10:07 PM New M-PESA balance is Ksh10,103.45. Separate personal and business funds through dummytext la dummytext on *377#.";

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

        if(matcher.find()) {
            String code = matcher.group(1);
            String amountReceived = matcher.group(2);
            String from = matcher.group(3);
            String date = matcher.group(4);
            String time = matcher.group(5);

            String format = "code: %s amount received: %s from: %s date: %s time: %s";
            System.out.println(String.format(format, code, amountReceived, from, date, time));
        }
    }
}

上述程序的输出:

代码语言:javascript
复制
code: PF56S55yy amount received: 6,495.00 from: Guaranty Trust Bank Limited 910201 date: 5/6/21 time: 10:07
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67946445

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档