正则表达式匹配模式,最高为“.”

本文关键字:模式 正则表达式 | 更新日期: 2023-09-27 17:56:29

我正在尝试使用正则表达式将字符串与句点匹配。例如,我有这个字符串

Updated IVR Info response to :Answered but No Response. Locked for IVR processing. 
Updated IVR Info response to :Yes. Locked for IVR processing.
Updated IVR Info response to :No. Locked for IVR processing. 

我希望我的匹配集合包含 3 个字符串。

Answered but No Response
No
Yes

我尝试过..

string pattern = "Updated IVR Info response to :.+?/.";
Regex r = new Regex(pattern);
var matches = r.Matches(item.Information);

正则表达式匹配模式,最高为“.”

您可以在正则表达式中使用回溯:

string pattern = "(?<=Updated IVR Info response to :)[^.]+";

正则表达式演示

我认为你正在以错误的方式逃避你的".":

"Updated IVR Info response to :.+?''.";

若要获取实际值,请使用组:

string pattern = "Updated IVR Info response to :(.+?''.)";
Regex r = new Regex(pattern);
var values = r.Matches(item.Information).Cast<Match>().Select(m => m.Groups[1].Value);

输出:

回答但没有回应。
是的。
不。