我正在尝试编写一个电子邮件主题行解析器,用户可以在其中定义自己的解析规则。规则将匹配主题行上的成员名称,然后将其用作查找。问题是成员名称可能包含解析规则分隔符。
// This rule has defined that any text between > matches a member name.
// Note the user can make up any parsing rule so this is just an example.
string sampleRule = ">{member}>";
// left out parsing code. We have figured out we are looking
// for member and the prefix/postfix delimiters.
string prefix = ">";
string postfix = ">";
// note that member>Name3 is a valid member name
string subject = "Subject>memberName1>memberName2>member>Name3>EndSubject";
string pattern = "(?="+prefix+"([a-z].+?)"+postfix+")";
Match m = Regex.Match(subject, pattern);
while(m.Success) {
// this is a possible member name
Console.WriteLine(m.Groups[1].ToString());
m = m.NextMatch();
}
// the output needs to be
// memberName1
// memberName2
// Member>Name3
// It is currently
// memberName1
// memberName2
// member
// Note that spanning bad matches are ok, for example
// memberName1>memberName2 or memberName1>memberName2>member>Name3