我正在开发一个Android APP,它有一个基于短信传入消息的广播接收器。
我想跟踪来自特定senderNumber的每条消息,并使用该SMS执行某些操作,例如,从每条消息中检索一些数据。
我要分析的消息体是:
"已使用ha recibido 5.0 CUC del numero 55391393 .Saldo principal 1565.0 CUC,linea activa hasta 2019-02-10,vence 2019-03-12"
我想用Pattern类提取,标记为粗体的值。但我在正则表达式中真的很新。一些帮助?
这是我的实际代码:
public class SMSReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
final Bundle bundle = intent.getExtras();
try {
if (bundle != null) {
final Object[] pdusObj = (Object[]) bundle.get("pdus");
assert pdusObj != null;
for (Object aPdusObj : pdusObj) {
SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) aPdusObj);
String senderNum = currentMessage.getDisplayOriginatingAddress();
String message = currentMessage.getDisplayMessageBody();
/*
String body = currentMessage.getMessageBody().toString();
String address = currentMessage.getOriginatingAddress();
*/
Log.i("SmsReceiver", "senderNum: " + senderNum + "; message: " + message);
//Save to DB
if (senderNum.equals("Cubacel")) {
Toast.makeText(context, "senderNum: " + senderNum + ", message: " + message, Toast.LENGTH_LONG).show();
//Parse this SMS with Regular Expresions
} else {
//Search for transferred numbers pending
}
} // end for loop
} // bundle is null
} catch (Exception e) {
Log.e("SmsReceiver", "Exception smsReceiver" + e);
}
}
}
这是一个使用JS的示例工作代码,但我不知道如何在Java中实现 https://regexr.com/3mgq2
答案 0 :(得分:1)
String re1=".*?"; // Non-greedy match on filler
String re2="(5\\.0)"; // Float 1
String re3=".*?"; // Non-greedy match on filler
String re4="(55391393)"; // Number 1
String re5=".*?"; // Non-greedy match on filler
String re6="(1565\\.0)"; // Float 2
Pattern p = Pattern.compile(re1+re2+re3+re4+re5+re6,Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
试试这个:)
答案 1 :(得分:0)
我想我明白了,请告诉我是否有更好的方法:
[[Model]]
(Model(gaussian_cdf, prefix='g1_') + Model(gaussian_cdf, prefix='g2_'))
[[Fit Statistics]]
# fitting method = leastsq
# function evals = 66
# data points = 50
# variables = 6
chi-square = 0.00626332
reduced chi-square = 1.4235e-04
Akaike info crit = -437.253376
Bayesian info crit = -425.781238
[[Variables]]
g1_amp: 0.65818908 +/- 0.00851338 (1.29%) (init = 0.5)
g1_mu: 93.8438526 +/- 0.01623273 (0.02%) (init = 94)
g1_sigma: 0.54362156 +/- 0.02021614 (3.72%) (init = 1)
g2_amp: 0.34058664 +/- 0.01153346 (3.39%) (init = 0.5)
g2_mu: 97.7056728 +/- 0.06408910 (0.07%) (init = 98)
g2_sigma: 1.24891832 +/- 0.09204020 (7.37%) (init = 1)
[[Correlations]] (unreported correlations are < 0.100)
C(g1_amp, g2_amp) = -0.892
C(g2_amp, g2_sigma) = 0.848
C(g1_amp, g2_sigma) = -0.744
C(g1_amp, g1_mu) = 0.692
C(g1_amp, g2_mu) = 0.662
C(g1_mu, g2_amp) = -0.607
C(g1_amp, g1_sigma) = 0.571