0

在我的应用程序中,我必须扫描所有用户短信并根据一些关键字对其进行过滤。因为我可以smsBody.contains(filterKey)直接使用。但是假设过滤键就像你已经获得了<>+Rewards,其中<>应该有一些数字。即,您已获得 1000 奖励,如果此字符串出现在短信中,我必须拒绝。我在下面添加了我的代码

if (!shouldIgnore(smsBody)) {
  //further process
} else {
    LogUtils.LOGD(TAG, "ignoring message : " + smsBody);
}



public boolean shouldIgnore(String body) {
    ListIterator<String> listIterator =mList.listIterator()

    while(listIterator.hasNext()){
      String key = listIterator.next();

      if(smsBody.contains(key)){
        return true;
    }
}
return false
}

String.contains() 只有在准确的键出现时才会返回 true,但在这种情况下,键可能不准确。我该如何修改这个方法?

4

1 回答 1

2

我不确定是否能很好地理解你,但如果我错过了什么,请告诉我。正则表达式将完成这项工作:

public boolean shouldIgnore(String body) {
   Pattern p = Pattern.compile("you have earned [0-9]+ Rewards");
   Matcher m = p.matcher(body);
   return m.find();
}

[0-9]+you have earned查找和之间的任何整数,如果在 中找到模式,则RewardsMatcher 将返回,否则,将返回。truebodyfalse

此外,您可以使用String.matches注释中解释的方法。

于 2015-12-16T11:05:48.600 回答