Empty string in regex c #

I have the following Regex template: "[A-TVWZ]" . I need it to accept also the empty "" lines, how can I insert this into my template? I need this for my DataGridView, because when the User leaves the cell without writing anythig, it gets an error while checking ...

+4
source share
3 answers

Perhaps this is too simple; I would check for empty strings without regular expressions.

eg:.

 if ( string.IsNullOrEmpty( myString ) || Regex.IsMatch( "[A-TVWZ]", myString ) { .... } 

See:

+9
source

Try "[A-TVWZ]?" or "^[A-TVWZ]?$"

The Question icon ( ? ) Ensures that the pattern matches zero or 1 time. So zero time means an empty string.

+5
source

A pure regular expression of this would be

 [A-TVWZ]|^$ 

which will either match one of the above letters, or an empty string. Your comment indicates that the user can enter only one character, so another option would be

 ^[A-TVWZ]?$ 

which is similar to Wakas answer. However, this one will not enter other lines that may coincide due to anchors.

+2
source

Source: https://habr.com/ru/post/1412491/


All Articles