Regex.IsMatch returns two different results in C #
I am trying to find a match between a string and a pattern using Regex.IsMatch (). I created a regex and tested it with regex101.com, it works great. Now the problem is that
Regex.IsMatch(filename, curSetting.RegExMatch.ToString());
returning true
curSetting.RegExMatch.IsMatch(filename))
returns false for the same filename. Can anyone explain how this is different and what is the difference between the two?
RegExMatch
is my object regex member curSetting
. The test data in my case is Pattern
Gen(?!.*(?:erallog))(?<SerialNo>.+?)-(?<Year>(?:\d{2}))(?<Month>\d{2})(?<Day>\d{2})(?<Other>.*?\.log)
String 1_GeneralLog1370013-170403.log
.
source to share
It is understood that yours is curSetting.RegExMatch
compiled with the flag RegexOptions.IgnoreCase
: is (?!.*(?:erallog))
treated case insensitively and matches eralLog
in your input string 1_GeneralLog1370013-170403.log
, so the negative reverse lookup pattern finds a match and a complete match.
So, there are 2 outputs (depending on what you want):
- Either remove
RegexOptions.IgnoreCase
from the regex object initialization, or -
Add a custom option
(?i)
to case:(?i)Gen(?!.*(?:erallog))(?<SerialNo>.+?)-(?<Year>(?:\d{2}))(?<Month>\d{2})(?<Day>\d{2})(?<Other>.*?\.log)
source to share