c# - Regex to check whether "and,or,not,and not" in a word? -
c# - Regex to check whether "and,or,not,and not" in a word? -
i have seneario have check word contains "and,or,not,and not" regex have created fails. can body provide me right regex this?
the regex have created this
regex objalphapattern = new regex(@"^[not|and not|and|not]"); if(objalphapattern.ismatch(searchterm)) { //// code }
but returns true.
i have tried word "pen , pencil" , "pen pencil" both returning true.. can help in providing right regex?
you're starting begin anchor. if don't want check if happens @ origin of string shouldn't have ^.
also, using [] when should using (). in case don't need (). [] indicates character class. don't need that.
regex objalphapattern = new regex("\b(and|not)\b"); if(objalphapattern.ismatch(searchterm)) { //// code }
that should job.
i highly recommend the regex coach help build regex.
i highly recommend http://www.regular-expressions.info/ reference.
edit: sense should point out don't need object instance.
if(system.text.regularexpressions.regex.ismatch(searchterm, "\b(and|not)\b")) { //// code }
you can utilize static method.
that's point tim:
"\band\b|\bnot\b"
another point stema:
"\b(and|not)\b"
c# regex
Comments
Post a Comment