Regex r = new Regex(@"(?<TM>[^.]*TEST.*)", RegexOptions.IgnoreCase);
First, as @manojlds said, you should use verbatim strings for regexes whenever possible. Otherwise you'll have to use two backslashes in most of your regex escape sequences, not just one (e.g. [!\\..]*
).
Second, if you want to match anything but a dot, that part of the regex should be [^.]*
. ^
is the metacharacter that inverts the character class, not !
, and .
has no special meaning in that context, so it doesn't need to be escaped. But you should probably use \w*
instead, or even [A-Z]*
, depending on what exactly you mean by "word". [!\..]
matches !
or .
.
Regex r = new Regex(@"(?<TM>[A-Z]*TEST[A-Z]*)", RegexOptions.IgnoreCase);
That way you don't need to bother with word boundaries, though they don't hurt:
Regex r = new Regex(@"(?<TM>\b[A-Z]*TEST[A-Z]*\b)", RegexOptions.IgnoreCase);
Finally, if you're always taking the whole match anyway, you don't need to use a capturing group:
Regex r = new Regex(@"\b[A-Z]*TEST[A-Z]*\b", RegexOptions.IgnoreCase);
The matched text will be available via Match's Value
property.
\w
matches [0-9a-zA-Z_]. If you don't want numbers or underscores, stick with\b
. – Damages