从交易短信中获取商家名称 - Android
Fetch Merchant Name from Transaction SMS - Android
我想从银行交易短信中获取商户名称。为此,我使用了以下 regx;
Pattern.compile("(?i)(?:\sat\s|in\*)([A-Za-z0-9]*\s?-?\s?[A-Za-z0-9]*\s?-?\.?)")
它适用于那些包含 at / in 的 SMS,但如果 SMS 包含这两个词以外的内容怎么办。
例如,短信就像 ;
Dear Customer, You have made a Debit Card purchase of INR1,600.00 on
30 Jan. Info.VPS*AGGARWAL SH.
那如何从上面的短信中获取AGGARWAL SH?
您可以使用下面的代码从字符串中获取商户名称
private void extractMerchantNameFromSMS(){
try{
String mMessage= "Dear Customer, You have made a Debit Card purchase of INR1,600.00 on 30 Jan. Info.VPS*AGGARWAL SH.";
Pattern regEx = Pattern.compile("(?i)(?:\sInfo.\s*)([A-Za-z0-9*]*\s?-?\s?[A-Za-z0-9*]*\s?-?\.?)");
// Find instance of pattern matches
Matcher m = regEx.matcher(mMessage);
if(m.find()){
String mMerchantName = m.group();
mMerchantName = mMerchantName.replaceAll("^\s+|\s+$", "");//trim from start and end
mMerchantName = mMerchantName.replace("Info.","");
FileLog.e(TAG, "MERCHANT NAME : "+mMerchantName);
}else{
FileLog.e(TAG, "MATCH NOTFOUND");
}
}catch(Exception e){
FileLog.e(TAG, e.toString());
}
}
我想从银行交易短信中获取商户名称。为此,我使用了以下 regx;
Pattern.compile("(?i)(?:\sat\s|in\*)([A-Za-z0-9]*\s?-?\s?[A-Za-z0-9]*\s?-?\.?)")
它适用于那些包含 at / in 的 SMS,但如果 SMS 包含这两个词以外的内容怎么办。 例如,短信就像 ;
Dear Customer, You have made a Debit Card purchase of INR1,600.00 on 30 Jan. Info.VPS*AGGARWAL SH.
那如何从上面的短信中获取AGGARWAL SH?
您可以使用下面的代码从字符串中获取商户名称
private void extractMerchantNameFromSMS(){
try{
String mMessage= "Dear Customer, You have made a Debit Card purchase of INR1,600.00 on 30 Jan. Info.VPS*AGGARWAL SH.";
Pattern regEx = Pattern.compile("(?i)(?:\sInfo.\s*)([A-Za-z0-9*]*\s?-?\s?[A-Za-z0-9*]*\s?-?\.?)");
// Find instance of pattern matches
Matcher m = regEx.matcher(mMessage);
if(m.find()){
String mMerchantName = m.group();
mMerchantName = mMerchantName.replaceAll("^\s+|\s+$", "");//trim from start and end
mMerchantName = mMerchantName.replace("Info.","");
FileLog.e(TAG, "MERCHANT NAME : "+mMerchantName);
}else{
FileLog.e(TAG, "MATCH NOTFOUND");
}
}catch(Exception e){
FileLog.e(TAG, e.toString());
}
}