检查电子邮件是否包含正则表达式

check if an email contains regex expresions

我必须提取电子邮件中包含的所有匹配字符串。

格式很简单,8 位数字,但是当我测试它时,它曾经跳过最后一个匹配的字符串。

模板电子邮件可以是电子邮件:Hi 12345678 87654321

它returns参考[] = ('12345678')

  public static String[] identificarsrNumber(Messaging.InboundEmail email){
    
    String[] reference= new List<String>();
    if (email.plainTextBody != null) {
        String[] splitemail = email.plainTextBody.split(' ');
        Pattern pt = Pattern.compile('^\d{8}$');
        for (String s : splitemail) {
            Matcher m = pt.matcher(s);
            if (m.find()) {
                reference.add(s);
            }
        }
     }
  return reference;
  }

您的电子邮件是否包含行尾或回车符 return 字符? 我猜最后 8 位数字部分不匹配,因为它被读作 87654321\n 并且您仅搜索 8 位数字(通过在模式的数字匹配器周围使用 ^ 和 $)

您可以确保将字符串与所有 Unicode 空白字符分开:

String[] splitemail = email.plainTextBody.split('(?U)\s+');

在这种情况下,(?U) 代表使 shorthand 字符 类 识别 Unicode 的 Pattern.UNICODE_CHARACTER_CLASS 选项。因此,\s 开始匹配各种 Unicode 空格。