-2

I do have the following code

Regex rx = new Regex(@"(\w{2}(\,\w{2})*)+");

I don't know how to check if the entire input string falls into that format or not for example

INPUT: W2 -> true
INPUT x3,4e -> true
INPUT x3,4e,33 -> true

INPUT: x -> false
INPUT x3,e -> false

I do not need to find any matches! I just need to know if the input is in the correct format or not.

Thanks

user3375740
  • 245
  • 1
  • 3
  • 11

2 Answers2

1

@Steve Howard & @Juharr

Thank you guys, worked !

Regex rx = new Regex(@"^(\w{2}(\,\w{2})*)+$");
            string input = txtTermCodes.Text.Trim();
            if(rx.IsMatch(input))
                return true;
            else 
                return false;
user3375740
  • 245
  • 1
  • 3
  • 11
0

You have to extract the result of the match, which is a string, then compare its length to the length of your input, like so:

Regex rx = new Regex(@"(\w{2}(\,\w{2})*)+");
String input = "Your input";
Match m = rx.Match(input);

if (!m.Success) return false;

return m.Length == input.Length;
Balázs
  • 2,929
  • 2
  • 19
  • 34