Why does this regex with no special character match a longer string?

前端 未结 4 1035
有刺的猬
有刺的猬 2021-01-23 00:21

I am using this method to try find a match, in an example:

Regex.Match(\"A2-TS-OIL\", \"TS-OIL\", RegexOptions.IgnoreCase).Success;

I got a tru

4条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-23 00:56

    If you only want a match if the test string starts with your regular expression, then you need to indicate as such:

    Regex.Match("A2-TS-OIL", "^TS-OIL", RegexOptions.IgnoreCase).Success;
    

    The ^ indicates that the match must start at the beginning of the string.

提交回复
热议问题