简体   繁体   English

使用 java 中的正则表达式从字符串中提取字符串值

[英]Extract string values from a string using regex in java

I am trying to extract information from a message on an android application using regex which I am not quite good at yet.我正在尝试使用我不太擅长的正则表达式从 android 应用程序的消息中提取信息。

The information I need is highlighted in bold from the following string.我需要的信息从以下字符串中以粗体突出显示。

PFEDDTYGD Confirmed.on 14/6/21 at 12:46 PMKsh 260.00 received from 254725400049 JOHN DOE . PFEDDTYGD Confirmed.on 14/6/21 at 12:46 PMKsh 260.00254725400049 JOHN DOE收到。 New Account balance is Ksh1,666.新账户余额为 Ksh1,666。 Transaction cost, Ksh1交易成本,Ksh1

code: PFEDDTYGD, date: 14/6/21, time:12:46, amountreceived: 260.00, phone no:254725400049 customer: JOHN DOE代码:PFEDDTYGD,日期:14/6/21,时间:12:46,收款:260.00,电话:254725400049 客户:JOHN DOE

here is my code: NB: the string is in multiline format.这是我的代码: 注意:字符串是多行格式。

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 string2 = "PFEDDTYG0D Confirmed.on 14/6/21 at 12:46PMKsh260.00 received from 254725400049 JOHN DOE. New Account balance is Ksh1,666. Transaction cost, Ksh1";
        final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
        final Matcher matcher = pattern.matcher(string2);

        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));

The pattern that you tried has parts in it that are not in the example data, and in some parts do not match enough characters.您尝试的模式中有部分不在示例数据中,并且在某些部分不匹配足够的字符。

You could update the pattern to 6 capture groups as:您可以将模式更新为 6 个捕获组,如下所示:

^([a-zA-Z0-9]+)\s+Confirmed\.on\h+(\d{1,2}/\d{1,2}/\d\d)\h+at\h+(\d{1,2}:\d{1,2})\w*Ksh(\d+(?:\.\d+)?).*?\bfrom\h+(\d+)\h+([^.]+)\.

See a Java demo and a regex demo请参阅Java 演示正则表达式演示

final String regex = "^([a-zA-Z0-9]+)\\s+Confirmed\\.on\\h+(\\d{1,2}/\\d{1,2}/\\d\\d)\\h+at\\h+(\\d{1,2}:\\d{1,2})\\w*Ksh(\\d+(?:\\.\\d+)?).*?\\bfrom\\h+(\\d+)\\h+([^.]+)\\.";

final String string2 = "PFEDDTYG0D Confirmed.on 14/6/21 at 12:46PMKsh260.00 received from 254725400049 JOHN DOE. New Account balance is Ksh1,666. Transaction cost, Ksh1";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string2);

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

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

Output Output

code: PFEDDTYG0D amount received: 260.00 from: JOHN DOE date: 14/6/21 time: 12:46 tel: 254725400049

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

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