简体   繁体   中英

Regex match pattern up to a “.”

I'm trying to use a regex to match a string up to a period. For example I have this string

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. 

I want my match collection to contain 3 strings.

Answered but No Response
No
Yes

What i tried..

string pattern = "Updated IVR Info response to :.+?/.";

Regex r = new Regex(pattern);

var matches = r.Matches(item.Information);

You can use a lookbehind in your regex:

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

RegEx Demo

I think you're escaping your '.' the wrong way:

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

To get the actual values, use a group:

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);

Output:

Answered but No Response.
Yes.
No.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM