开发者

How can I check if a string ends in is or are valid? using regex

开发者 https://www.devze.com 2023-04-02 03:30 出处:网络
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\"?

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")?

0

精彩评论

暂无评论...
验证码 换一张
取 消