I want to know how I can pass an OR regex expression. I tried "(u|^upload)"
but this doesn't seem to work and instead catches anything I type
var regex = new Regex("(u|^upload)", RegexOptions.IgnoreCase);
return regex.IsMatch(msg.Text);
I expect it to catch u or U or upload or Upload
The correct pattern would be:
^(u|upload)$
Where ^
and $
are anchors that match the start and end of the string, respectively. This means that the pattern will only match the entire string or nothing at all.
But for that matter, you don't really need regular expressions at all:
var values = new[] { "u", "upload" };
return values.Contains(msg.Text, StringComparer.OrdinalIgnoreCase);
Just for illustration, an equivalent pattern would be:
^u(pload)?$
This will mach any string that starts with u
, optionally followed by pload
, followed by the end of the string.
Note however, neither of these will just match u
, U
, upload
, or Upload
. Thanks to RegexOptions.IgnoreCase
, they would also match UPLOAD
or uPlOaD
. If you only want to match exactly those four options you could do:
var regex = new Regex("^[uU](pload)?$");
Or (using group options ):
var regex = new Regex("^u(?-i:pload)?$", RegexOptions.IgnoreCase);
Or without regular expressions:
var values = new[] { "u", "U", "upload", "Upload" };
return values.Contains(msg.Text);
Test this to match the strings start with 'u' or 'U',and end with 'e' or 'E'.And the charactor 'u' or 'U'.
([uU].*?e)|([uU])
Or you can use this to match 'update' , 'Update' , 'u' and 'U'.
([uU]pdate)|([uU])
To capture both u and U or upload and Upload use this
([uU]|[uU]pload)
If you want it at the beginning of the string:
^([uU]|[uU]pload)
If you want this to be checked against the entire string use:
^([uU]|[uU]pload)$
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.