How do I match an entire string with a regex?

前端 未结 7 1189
渐次进展
渐次进展 2020-11-22 00:15

I need a regex that will only find matches where the entire string matches my query.

For instance if I do a search for movies with the name \"Red October\" I only wa

7条回答
  •  爱一瞬间的悲伤
    2020-11-22 00:35

    Use the ^ and $ modifiers to denote where the regex pattern sits relative to the start and end of the string:

    Regex.Match("Red October", "^Red October$"); // pass
    Regex.Match("The Hunt for Red October", "^Red October$"); // fail
    

提交回复
热议问题