c# - regex works in online regex tester but not in .NET -
the following demo works fine online not when attempt run in c#/.net
var regex = new regularexpressionattribute(@"@(?!.*?\.\.)[^@]+$"); assert.istrue(regex.isvalid("bob@bob.com"));
here link post explain regex , trying
the regularexpressionattribute requires full match of string.
in case, matching starting @ @
character, start somewhere in between of string. not enough regularexpressionattribute. can think of adding implicit ^
@ start expression (and implicit $
@ end too, can drop well).
so fix this, need make sure has match @ start to, e.g. .*?
:
new regularexpressionattribute(@".*?@(?!.*?\.\.)[^@]+");
and works. don’t need other regular expression flags.
unfortunately, behavior of regularexpressionattribute
mentioned in documentation. can verify in reference source however:
// looking exact match, not search hit. matches // regularexpressionvalidator control return (m.success && m.index == 0 && m.length == stringvalue.length);
so it’s technically not added ^
, $
after finding match, start , end index required match string’s start , end index (which same thing).
and if think it, behavior makes lot of sense validation. after all, want make sure string matches full-specified format, , not check if substring valid.