0

I'm currently trying to implement a check for a regex matching an input string, which i would like to only validate letters a-z, (lower or caps) excluding all numbers, special characters...

However, if the regex matches partially, Regex.Match(input) will return true. I would like it to return false unless the entire input string is valid, how would i go about this?

Here is an example if how i would like it to work, the Regex pattern shown is what i'm currently using.

Regex expression = new Regex("^[a-zA-Z]*");

if(!expression.IsMatch("example123")) { /* do something */ }

As it is right now, the Regex will match up to "example", and still return true, even though it contains "123" at the end.

rld001
  • 153
  • 1
  • 3
  • 11
  • If you want to do it with a regex, use `"^[a-zA-Z]*$"`, or even better `@"^[a-zA-Z]*\z"`. – Wiktor Stribiżew Sep 11 '17 at 12:38
  • @WiktorStribiżew - If you're going to use `\z`, wouldn't it also be better to use `\A` in place of `^`? I've actually never used `\z` or `\A`, so I'm curious. – Broots Waymb Sep 11 '17 at 12:57
  • `\A` is only good if the user can modify the regex and pass the `RegexOptions.Multiline` option (say, with the help of `(?m)` inline modifier). If the pattern cannot be modified, `^` is equally good. – Wiktor Stribiżew Sep 11 '17 at 12:58

0 Answers0