regex 为什么正则表达式不匹配带空参数的方法?

5jvtdoz2  于 2023-03-20  发布在  其他
关注(0)|答案(1)|浏览(123)

我有下面的正则表达式,它匹配第三个参数为“null”或“string.empty”的函数“searchresult”:

^(.*\.)?searchresult\((?:[^,]*,){2}\s*(null|string\.Empty)\s*(?:,[^,]*){2}\);

正则表达式不应与以下行匹配

errorList.Add(new Microsoft.Practices.EnterpriseLibrary.Validation.searchresult("[Validation_MaxLength_ErrorMessage_2000]", staffonlineInquiry, "onlineInquiryResponseInformation", string.Empty, null));
searchresult error = new searchresult("[Validation_notwellLeavecheckReturnToWorkDateYes_ErrorMessage]|[Label_IsnotwellLeavecheckReturnToWorkDate_Text]", target, "notwellLeavecheckReturnToWorkDate", null, null);

正则表达式应与以下行匹配

errorList.Add(new Microsoft.Practices.EnterpriseLibrary.Validation.searchresult("[Validation_MaxLength_ErrorMessage_2000]", enquiry, null, string.Empty, null));
searchresult error = new searchresult("[Validation_notwellLeavecheckReturnToWorkDateYes_ErrorMessage]|[Label_IsnotwellLeavecheckReturnToWorkDate_Text]", target, string.Empty, null, null);
s4n0splo

s4n0splo1#

正则表达式给出了问题前三行的预期结果,第四行不匹配的原因是(并且在第二个上也永远不会匹配)的一个缺点是,正则表达式坚持searchresult(前面是.或行首(^),而在第二行和第四行,这是不正确的(它前面有一个空格)。

^(.*\.)?

变成了

^.*?(?:\.|new\s+)

这允许searchresult(.new之后。
regex101上的演示

相关问题