Reputation: 71
I'm trying to find match between a string and pattern using Regex.IsMatch(). I framed the regex and tested it using regex101.com it works fine. Now problem is that
Regex.IsMatch(filename, curSetting.RegExMatch.ToString());
returns true
curSetting.RegExMatch.IsMatch(filename))
returns false for same filename. Can someone explain how does this differ and what is the difference between them?
RegExMatch
is my regex member of curSetting
object. Test data in my case is Pattern is
Gen(?!.*(?:erallog))(?<SerialNo>.+?)-(?<Year>(?:\d{2}))(?<Month>\d{2})(?<Day>\d{2})(?<Other>.*?\.log)
The string is 1_GeneralLog1370013-170403.log
.
Upvotes: 3
Views: 358
Reputation: 626893
It is clear that your curSetting.RegExMatch
is compiled with a RegexOptions.IgnoreCase
flag: the (?!.*(?:erallog))
is handled case-insensitively and matches the eralLog
in your 1_GeneralLog1370013-170403.log
input string, so the negative lookahead pattern finds a match and fails the overall match.
So, there are 2 ways out (depending on what you need):
RegexOptions.IgnoreCase
from the regex object initialization orAdd the case-insensitive inline option (?i)
into the pattern:
(?i)Gen(?!.*(?:erallog))(?<SerialNo>.+?)-(?<Year>(?:\d{2}))(?<Month>\d{2})(?<Day>\d{2})(?<Other>.*?\.log)
Upvotes: 2