G
Guest
By working with RegularExpressions I found some inconsistency between the
Escape and Match method.
Has anyone an explanation for this behaviour or is this a bug?
Following the description of the problem:
'Escape' DOES NOT recognize ']' as a metacharacter.
'Match(es)' DOES recognize ']' as a metacharacter.
void Wanted_but_not_working()
{
string OpenMask = "[";
string CloseMask = "]";
OpenMask = Regex.Escape(OpenMask);
OpenMask = Regex.Escape(OpenMask);
//Expected Result: OpenMask="\\\[";
CloseMask = Regex.Escape(CloseMask);
CloseMask = Regex.Escape(CloseMask);
//Expected Result: CloseMask="\\\]";
//Why is ']' no metacharacter?
//It has to be. Otherwise the Match below should work.
//Either the Escape or the Match operation does not work properly.
string actualDefinition = "[KG]*[IND]*[VHW]*[KZ]";
actualDefinition = Regex.Escape(actualDefinition);
//Expected Result: "\[KG\]\*\[IND\]\*\[VHW\]\*\[KZ\]"
string pattern =
String.Concat(OpenMask,"[^",OpenMask,CloseMask,"]+",CloseMask);
//Expected Result: "\\\[[^\\\[\\\]]+\\\]"
MatchCollection ma = Regex.Matches(actualDefinition, pattern,
RegexOptions.Compiled | RegexOptions.CultureInvariant |
RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
//Expected Result: 4 matches... NOTHING
}
void Manual_and_working_but_not_wanted()
{
string OpenMask = @"\\\[";
string CloseMask = @"\\\]";
string actualDefinition = @"\[KG\]\*\[IND\]\*\[VHW\]\*\[KZ\]";
string pattern = String.Concat(OpenMask, "[^", OpenMask, CloseMask, "]+",
CloseMask);
MatchCollection ma = Regex.Matches(actualDefinition, pattern,
RegexOptions.Compiled | RegexOptions.CultureInvariant |
RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
//Expected Result: 4 matches ... OK
}
void Crazy_Workaround()
{
string OpenMask = "[";
string CloseMask = @"\]";
OpenMask = Regex.Escape(OpenMask);
OpenMask = Regex.Escape(OpenMask);
//Expected Result: OpenMask="\\\[";
string actualDefinition = "[KG]*[IND]*[VHW]*[KZ]";
actualDefinition = Regex.Escape(actualDefinition);
string pattern = String.Concat(OpenMask, "[^", OpenMask, CloseMask, "]+",
CloseMask);
MatchCollection ma = Regex.Matches(actualDefinition, pattern,
RegexOptions.Compiled | RegexOptions.CultureInvariant |
RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
//Expected Result: 4 matches... OK
}
Escape and Match method.
Has anyone an explanation for this behaviour or is this a bug?
Following the description of the problem:
'Escape' DOES NOT recognize ']' as a metacharacter.
'Match(es)' DOES recognize ']' as a metacharacter.
void Wanted_but_not_working()
{
string OpenMask = "[";
string CloseMask = "]";
OpenMask = Regex.Escape(OpenMask);
OpenMask = Regex.Escape(OpenMask);
//Expected Result: OpenMask="\\\[";
CloseMask = Regex.Escape(CloseMask);
CloseMask = Regex.Escape(CloseMask);
//Expected Result: CloseMask="\\\]";
//Why is ']' no metacharacter?
//It has to be. Otherwise the Match below should work.
//Either the Escape or the Match operation does not work properly.
string actualDefinition = "[KG]*[IND]*[VHW]*[KZ]";
actualDefinition = Regex.Escape(actualDefinition);
//Expected Result: "\[KG\]\*\[IND\]\*\[VHW\]\*\[KZ\]"
string pattern =
String.Concat(OpenMask,"[^",OpenMask,CloseMask,"]+",CloseMask);
//Expected Result: "\\\[[^\\\[\\\]]+\\\]"
MatchCollection ma = Regex.Matches(actualDefinition, pattern,
RegexOptions.Compiled | RegexOptions.CultureInvariant |
RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
//Expected Result: 4 matches... NOTHING
}
void Manual_and_working_but_not_wanted()
{
string OpenMask = @"\\\[";
string CloseMask = @"\\\]";
string actualDefinition = @"\[KG\]\*\[IND\]\*\[VHW\]\*\[KZ\]";
string pattern = String.Concat(OpenMask, "[^", OpenMask, CloseMask, "]+",
CloseMask);
MatchCollection ma = Regex.Matches(actualDefinition, pattern,
RegexOptions.Compiled | RegexOptions.CultureInvariant |
RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
//Expected Result: 4 matches ... OK
}
void Crazy_Workaround()
{
string OpenMask = "[";
string CloseMask = @"\]";
OpenMask = Regex.Escape(OpenMask);
OpenMask = Regex.Escape(OpenMask);
//Expected Result: OpenMask="\\\[";
string actualDefinition = "[KG]*[IND]*[VHW]*[KZ]";
actualDefinition = Regex.Escape(actualDefinition);
string pattern = String.Concat(OpenMask, "[^", OpenMask, CloseMask, "]+",
CloseMask);
MatchCollection ma = Regex.Matches(actualDefinition, pattern,
RegexOptions.Compiled | RegexOptions.CultureInvariant |
RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
//Expected Result: 4 matches... OK
}