Determine if the Regex object will only accept uppercase characters.

On the system I'm working on, regular expressions are used to provide a specific input format for WPF text fields.

Behavior receives a Regex object and manages entered characters and allows only valid ones. ( similar to this article )

However, there is one exception. When only uppercase characters are accepted, the characters entered must be automatically converted to uppercase instead of being rejected.

My question is:

How to elegantly determine that the regex provided in Regex will only accept upper case? Is the only way to check the lowercase string string and then the uppercase string on it? Example:

if (Regex.IsMatch("THIS SHOULD PASS") && !Regex.IsMatch("this should fail")
{
    // logic to convert lower case to upper case.
}
+4
source share
2 answers

I got bored and took a picture.

Here's a sad but elegant implementation.

This does not catch unicode or hexadecimal escape sequences.

There are probably some other errors. Do unit tests.

Feel free to expand it.

+1
source

How elegantly to determine that a regular expression will accept only uppercase?

[a-z], , :

Regex.Replace("OmegaMan", "[a-z]", (mt) =>
 {
    return mt.Groups[0].Value.ToUpper();
 }

OMEGAMAN

; .

0

All Articles