Outlook 电子邮件 - 正则表达式允许 Reply/Forward 主题中的电子邮件
Outlook Email - REGEX to allow Reply/Forward Emails in Subject
我需要 REGEX 才能在自动化中回复和转发电子邮件。如果 reply/forward 电子邮件带有票号 INCxxxxx|SRxxxx(例如 INC123456、SR45678),则需要允许此操作,否则(reply/forward 没有票号的电子邮件)应忽略此。
遵循的步骤列表:
- 将电子邮件主题转换为大写
- 使用 REGEX
RE:.*|FW:.*
允许 Reply/Forward 电子邮件
- 进一步使用 REGEX
.*INC[0-9]+.*|.*SR[0-9]+.*
来确定电子邮件主题中是否出现 TicketNo。
我知道当我们考虑 reply/forward 封电子邮件时,会出现多种组合(RE:|RE :|FW:|FW :|FWD:|FWD : 等)。
没有取票:
String input = "issue with outlook INC7681499";
String pattern = "(?i)(INC|SR)\d+";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(input);
while (m.find()) {
String ticketNo=m.group(1);
System.out.println("Found value: " + ticketNo );
}
还有其他最好的方法吗?
如果使用 (?i)
标志使正则表达式不区分大小写,则不必将电子邮件主题转换为大写。
您在正则表达式中对 |
的使用非常广泛,您实际上是在为每个选项指定一个完整的主题匹配项。您可以将其限制为仅在发生变化的部分之间使用“或”。
(?i)(RE|FWD?)\s*:
(?i)(INC|SR)\d+
如果你使用 Pattern
和 Matcher
类,你也可以用 Matcher.find()
取消两端的 .*
支持子字符串匹配。
子字符串匹配也将允许主题中的任何常见标签,例如 FYI/Urgent/Important RE/FWD:,以及 Matcher.group()
如果以后需要,您可以很容易地提取打勾的 ID。
您可以拍下票号。 (?i)(INC|SR)\d+
也可以通过调用 Matcher.group()
而不传递任何索引 或调用 Matcher.group(0)
.[=28 来获得完整的子字符串匹配=]
String input = "issue with outlook INC7681499";
String regex = "(?i)(INC|SR)\d+";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
while (matcher.find()) {
System.out.println("Ticket no. " + matcher.group()); // INC7681499
}
Capturing groups are numbered by counting their opening parentheses
from left to right. In the expression ((A)(B(C)))
, for example, there
are four such groups:
1. ((A)(B(C)))
2. (A)
3. (B(C))
4. (C)
Group zero always stands for the entire expression.
因此,使用嵌套括号 (?i)((INC|SR)\d+)
您最终将创建一个冗余捕获组:
0. (INC|SR)\d+
1. (INC|SR)\d+
2. (INC|SR)
我需要 REGEX 才能在自动化中回复和转发电子邮件。如果 reply/forward 电子邮件带有票号 INCxxxxx|SRxxxx(例如 INC123456、SR45678),则需要允许此操作,否则(reply/forward 没有票号的电子邮件)应忽略此。
遵循的步骤列表:
- 将电子邮件主题转换为大写
- 使用 REGEX
RE:.*|FW:.*
允许 Reply/Forward 电子邮件 - 进一步使用 REGEX
.*INC[0-9]+.*|.*SR[0-9]+.*
来确定电子邮件主题中是否出现 TicketNo。
我知道当我们考虑 reply/forward 封电子邮件时,会出现多种组合(RE:|RE :|FW:|FW :|FWD:|FWD : 等)。
没有取票:
String input = "issue with outlook INC7681499";
String pattern = "(?i)(INC|SR)\d+";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(input);
while (m.find()) {
String ticketNo=m.group(1);
System.out.println("Found value: " + ticketNo );
}
还有其他最好的方法吗?
如果使用 (?i)
标志使正则表达式不区分大小写,则不必将电子邮件主题转换为大写。
您在正则表达式中对 |
的使用非常广泛,您实际上是在为每个选项指定一个完整的主题匹配项。您可以将其限制为仅在发生变化的部分之间使用“或”。
(?i)(RE|FWD?)\s*:
(?i)(INC|SR)\d+
如果你使用 Pattern
和 Matcher
类,你也可以用 Matcher.find()
取消两端的 .*
支持子字符串匹配。
子字符串匹配也将允许主题中的任何常见标签,例如 FYI/Urgent/Important RE/FWD:,以及 Matcher.group()
如果以后需要,您可以很容易地提取打勾的 ID。
您可以拍下票号。 (?i)(INC|SR)\d+
也可以通过调用 Matcher.group()
而不传递任何索引 或调用 Matcher.group(0)
.[=28 来获得完整的子字符串匹配=]
String input = "issue with outlook INC7681499";
String regex = "(?i)(INC|SR)\d+";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
while (matcher.find()) {
System.out.println("Ticket no. " + matcher.group()); // INC7681499
}
Capturing groups are numbered by counting their opening parentheses from left to right. In the expression
((A)(B(C)))
, for example, there are four such groups:1. ((A)(B(C))) 2. (A) 3. (B(C)) 4. (C)
Group zero always stands for the entire expression.
因此,使用嵌套括号 (?i)((INC|SR)\d+)
您最终将创建一个冗余捕获组:
0. (INC|SR)\d+
1. (INC|SR)\d+
2. (INC|SR)