【发布时间】:2017-05-02 19:43:54
【问题描述】:
我正在尝试使用 Regex.IsMatch() 来查找字符串和模式之间的匹配。我构建了正则表达式并使用 regex101.com 对其进行了测试,它工作正常。现在的问题是
Regex.IsMatch(filename, curSetting.RegExMatch.ToString()); 返回真
curSetting.RegExMatch.IsMatch(filename)) 为相同的文件名返回 false。有人能解释一下这有什么不同吗?它们之间有什么区别?
RegExMatch 是我的curSetting 对象的正则表达式成员。我的测试数据是 Pattern is
Gen(?!.*(?:erallog))(?<SerialNo>.+?)-(?<Year>(?:\d{2}))(?<Month>\d{2})(?<Day>\d{2})(?<Other>.*?\.log)
字符串是1_GeneralLog1370013-170403.log。
【问题讨论】:
-
请分享完整的相关代码。
curSetting.RegExMatch模式是什么?filename的价值是什么? -
当您发布您正在搜索的模式和文本时会有所帮助。也许这会对您有所帮助Regular Expression working in regex tester, but not in c#
-
RegExMatch是什么类型?没听说过。 -
RegExMatch是我的curSetting对象的正则表达式成员。我的测试数据是 Pattern isGen(?!.*(?:erallog))(?<SerialNo>.+?)-(?<Year>(?:\d{2}))(?<Month>\d{2})(?<Day>\d{2})(?<Other>.*?\.log)string is1_GeneralLog1370013-170403.log -
I get true for both cases。你的
curSetting.RegExMatch编译的标志是什么?我猜你用过Regex.IgnoreCase。那么,你现在想要什么?