我已经创建了一个应用程序来监控收到的短信使用广播接收器。该应用程序监视来自特定发送方的特定消息。我试图从具有以下格式的信息中添加某些信息:
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
发布于 2021-06-12 07:38:38
使用regex,您可以提取所需信息。
在https://regex101.com/r/ifuwVg/1上尝试regex
Java代码:
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));
}
}
}上述程序的输出:
code: PF56S55yy amount received: 6,495.00 from: Guaranty Trust Bank Limited 910201 date: 5/6/21 time: 10:07https://stackoverflow.com/questions/67946445
复制相似问题