In C# i'm using RegexValidator to validate a field which can contains only L,l,M,m,D,d values. I tried to use [RegexValidator("[l|L][M|m][D|d]" ... , but this doe开发者_JAVA百科s not work. Any ideas?
Thanks
This regex:
[l|L][M|m][D|d]
means:
- l, | or L; then
- M, | or m; then
- D, | or d.
Try:
^[LMD]+$
as a case insensitive match if you can do that or:
^[LlMmDd]+$
if you can't.
Both of these require the whole string to match a sequence of L, l, M, m, D or d characters. Use +
to mean one or more if it can't be empty or *
to mean 0 or more if it's allowed to be empty.
Edit: based on the updated information, if you want to allow one of those characters and only one of those then:
^[LlMmDd]$
you can also use inversion. check the field for characters not in your defined list.
/[^LlMmDd]+/
If I understand you correctly then you can try something like this
var s1 = "Ll";
var s2 = "m";
var s3 = "LmD";
var pattern = "^[LMD]$";
Console.WriteLine( Regex.IsMatch(s1, pattern, RegexOptions.IgnoreCase) );
Console.WriteLine( Regex.IsMatch(s2, pattern, RegexOptions.IgnoreCase) );
Console.WriteLine( Regex.IsMatch(s3, pattern, RegexOptions.IgnoreCase) );
Writes the following to the console
False
True
False
You can only use Regex on string but you could always convert your char
char c = 'd';
Console.WriteLine( Regex.IsMatch(new String(c,1), pattern, RegexOptions.IgnoreCase));
This code should read a string from the console, and match it. It should accept only 1 character, and matches with the characters you provided.
static void Main(string[] args)
{
Regex regex = new Regex(@"^[L|M|D]$", RegexOptions.IgnoreCase);
System.Console.WriteLine("Enter Text");
String str = System.Console.ReadLine();
Match match = regex.Match(str);
if (match.Success == true)
{
System.Console.WriteLine("Success");
}
else
{
System.Console.WriteLine("Fail");
}
System.Console.ReadLine();
}
You need to read up on regexp. You should be able to Google (or Bing) on tutorials of regexp.
[asdf]
means one and only one and not less than one char which is either a
, s
, d
or f
.
[asdf]*
means any number of characters but they must be either be a
, s
, d
or f
.
[asdf]+
means at least one character and they must either be a
, s
, d
or f
.
Regexp does not use the |
character as an OR operator in within a []
square bracket character definition constraint.
[A-Z][a-z]+[0-9]
means a String that must have the first character uppercase alphabet (any uppercase char between A to Z) followed by at least one lowercase alphabet (any lowercase char between a
to z
) and terminated by a decimal digit.
[l|L][M|m][D|d]
means the string must have exactly 3 chars.
First char must be either l
, |
or L
.
2nd char must be either M
, |
or m
.
3rd char must be either D
, |
or d
.
[lL][Mm][Dd]
may be what you want to use.
[lLMmDd]+
for a string of at least one char constrained to be either l
, L
, M
, m
, D
or d
.
[lLMmDd][lLMmDd][lLMmDd]+
for a string of at least 3 chars long where all chars are constrained to be either l
, L
, M
, m
, D
or d
.
精彩评论