anthonypliu
anthonypliu

Reputation: 12437

Regular expression, specific string with symbol

I have the following regular expression in my data annotation:

[RegularExpression(@"test|test2|test3|test4-5", ErrorMessage = "Please select your status.")]

I want the value to accept "test4-5", but it doesnt seem to like the dash symbol, how would i make this accept the exact string "test4-5"

Upvotes: 1

Views: 528

Answers (2)

Adrian Thompson Phillips
Adrian Thompson Phillips

Reputation: 7158

All the answers seem almost there, what you actually need is:

"^(test|test2|test3|test4\-5)$"

Note: Brackets to include the whole word to be included in the logical OR and not just the characters around the pipe symbol. Escaped hyphen. Caret and dollar symbols mark the beginning and end of the string. Giving you an exact match on your allowed strings.

Upvotes: 1

Anirudha
Anirudha

Reputation: 32817

It should be

(test|test2|test3|test4)-5

Upvotes: 0

Related Questions