Joper
Joper

Reputation: 8219

Looking for Regex

I want to validate Winforms text box with regex.

The input sting example:

ZX1 OR N?V OR 2L? OR ?55 (any sequence of three symbols length strings with OR between them)

What is the regex that you would advise?

UPDATE: Trying this one but seams to be it is not 100% correct

string text = "ZX1 OR N?V OR 2L? OR ?55";
Regex r = new Regex("([0-9A-Z?]{3} OR )*[0-9A-Z?]{3}");

Upvotes: 0

Views: 97

Answers (3)

Mike Samuel
Mike Samuel

Reputation: 120546

"^\\s*\\S{3}(?:\\s+OR\\s+\\S{3})*\\s*$"

should work in a variety of languages.

\\S

matches any non-space character, and

\\s

matches any space character, so the regex above matches any number of triplets of non-space characters separated by the string "OR" surrounded by space characters.

The ^ and $ serve to ensure that it matches the whole string so you can take those out if you want to find this pattern inside a larger string.

Upvotes: 1

Monday
Monday

Reputation: 1413

try...

(([\w\S]{3}\s+)or\s+)+[\w\S]{3}

Upvotes: 0

Charles Ma
Charles Ma

Reputation: 49201

What is the list of possible symbols you can have? can you have at most one question mark? This will match what you've given, but it will also match multiple question marks.

([A-Z?]{3} OR )*[A-Z?]{3}

Upvotes: 0

Related Questions