จะแยกค่าหลายค่าจาก Regex ได้อย่างไร

Util.ClearResults();
string tst = String.Join("", DateRange.Take(10).Select(d => d.DocHistory));
var matches = Regex.Matches (tst, "(?:[a-zA-Z'-]+[^a-zA-Z'-]+){0,2}assigned by(?:[^a-zA-Z'-]+[a-zA-Z'-]+){0,2}", RegexOptions.Multiline);
matches.Dump("Regex Matches");
foreach(var match in matches)
{
    match.Dump("Ind Match");
}

ฉันมีรหัสนี้และดูเหมือนว่าจะทำงานได้อย่างถูกต้องในการจับ "ค่า" และไม่ได้แยกค่าเฉพาะออกจากนั้น:

ฉันมีสตริงดังต่อไปนี้: "คำขอปิดโดย Jack Arm เมื่อวันที่ 16/08/2018, Assignee #1 James Arye มอบหมายโดย Scotty Shep เมื่อวันที่ 16/08/2018, คำขอส่งโดย Mac Weaver เมื่อวันที่ 16/08/2018, สร้างคำขอแล้ว โดย Mac Weaver เมื่อวันที่ 16/08/2018"

ฉันกำลังพยายามแยกชื่อทางด้านซ้ายและด้านขวาของ "มอบหมายโดย" แต่ฉันได้รับ "James Arye มอบหมายโดย Scotty Shep"...มีวิธีที่จะแยก "คุณค่า" ออกหรือไม่ พบเป็น 3 ตัวแปรด้วย regex?


person Kelly Kleinknecht    schedule 26.08.2018    source แหล่งที่มา
comment
ใช้การจับกลุ่ม - ((?:[a-zA-Z'-]+[^a-zA-Z'-]+){0,2})assigned by((?:[^a-zA-Z'-]+[a-zA-Z'-]+){0,2}) - จากนั้น match.Groups[1].Value และ match.Groups[2].Value จะเก็บผลลัพธ์ไว้   -  person Wiktor Stribiżew    schedule 26.08.2018
comment
ข้อมูลอยู่ในบรรทัดเดียวหรือหลายบรรทัด?   -  person jdweng    schedule 26.08.2018
comment
@jdweng ข้อมูลสามารถมีได้หลายบรรทัด   -  person Kelly Kleinknecht    schedule 26.08.2018
comment
@Wiktor Stribizew - ทำได้ดีมาก นั่นคือสิ่งที่ฉันต้องการ   -  person Kelly Kleinknecht    schedule 26.08.2018
comment
@Wiktor Stribizew the match.Groups[1].Value ให้ข้อผิดพลาดในการคอมไพล์ การจับคู่นั้นไม่มี Groups...มีใครคิดบ้างไหม   -  person Kelly Kleinknecht    schedule 26.08.2018
comment
คุณแน่ใจหรือว่าคุณกำลังทำซ้ำคอลเลกชันการจับคู่อย่างถูกต้อง? ลอง foreach (Match match in matches) { ... }   -  person Wiktor Stribiżew    schedule 26.08.2018


คำตอบ (1)


คุณต้องใช้กลุ่มการจับบริเวณส่วนต่างๆ ที่คุณต้องการ:

((?:[a-zA-Z'-]+[^a-zA-Z'-]+){0,2})assigned by((?:[^a-zA-Z'-]+[a-zA-Z'-]+){0,2})
^ ---------- Group 1 ----------- ^           ^ ---------- Group 2-----------  ^

ดู การสาธิต Regex

ป้อนคำอธิบายรูปภาพที่นี่

การสาธิต C#:

var s = "Request closed by Jack Arm on 08/16/2018,Assignee #1 James Arye assigned by Scotty Shep on 08/16/2018,Request submitted by Mac Weaver on 08/16/2018,Request created by Mac Weaver on 08/16/2018";
var pattern = @"((?:[a-zA-Z'-]+[^a-zA-Z'-]+){0,2})assigned by((?:[^a-zA-Z'-]+[a-zA-Z'-]+){0,2})";
var matches = Regex.Matches(s, pattern);
foreach (Match match in matches)
{
    Console.WriteLine(match.Groups[1].Value.Trim());
    Console.WriteLine(match.Groups[2].Value.Trim());
}

เอาท์พุท:

James Arye
Scotty Shep
person Wiktor Stribiżew    schedule 26.08.2018