How can I check if a string ends in is or are valid? using regex
I do a check to see if my strings contain the words "is valid". But how usin开发者_Python百科g regex can I check "is valid" or "are valid"?
@"Which (of the following )?(is valid) (?<result>.*?)[?:]"
To match both "is valid" and "are valid", you can modify your regular expression like this:
@"Which (of the following )?(((is)|(are)) valid) (?<result>.*?)[?:]"
This will match is valid
or are valid
at the end of a string
"(is valid)|(are valid)$"
If you have a string of a lot of lines, you can do this:
new System.Text.RegularExpressions.Regex("((is)|(are)) valid\n");
But it would be an option, split the string into a array of lines (str.Split('\n')
), and the use EndsWith
in each line.
What's the problem with s.Contains("is valid") || s.Contains("are valid")
?
精彩评论