Reputation: 529
I want to fetch Merchant Name from Bank Transaction SMS. For this I used following regx;
Pattern.compile("(?i)(?:\\sat\\s|in\\*)([A-Za-z0-9]*\\s?-?\\s?[A-Za-z0-9]*\\s?-?\\.?)")
Its works fine with those SMS which contains at / in but what if SMS contains other than this two words. For example, SMS is like ;
Dear Customer, You have made a Debit Card purchase of INR1,600.00 on 30 Jan. Info.VPS*AGGARWAL SH.
Then how to fetch AGGARWAL SH from above SMS?
Upvotes: 1
Views: 1621
Reputation: 10887
You can use this below code to fetch Merchant Name from String
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());
}
}
Upvotes: 1