0

I have a very basic Regex that I want to determine whether something is alphanumeric or not. When I pass it SPX$ though, it matches still - even though I would have expected it to be false.

The failing line is simple

Regex.IsMatch("SPX$", "[a-zA-Z0-9]+")

What am I doing wrong?

NZJames
  • 4,963
  • 15
  • 50
  • 100
  • You regex isn't anchored, so it matches the `SPX` bit and just ignores the `$`. Anchor it: `^[a-zA-Z0-9]+$` – canton7 Feb 27 '20 at 09:49
  • Missing `^` / `\A` at the start and `$` / `\z` at the end. Use `\z` ([suggested here](https://stackoverflow.com/a/4123738/3832970)), it matches the very end of string. `$` may also match before a final newline in the string (even without the `RegexOptions.Multiline`), and most users do not expect this behavior. – Wiktor Stribiżew Feb 27 '20 at 09:49

0 Answers0