I need to check if entire given input matches given pattern.
But wrapping a pattern in^
/$
feels like a hack.
Is there a shortcut for:
var match = Rege开发者_Go百科x.Match(myInput, "^" + myPattern + "$");
?
There is no shortcut, and adding ^
and $
is not a hack. What you're doing is exactly what you're supposed to do in order to match an entire line.
If it makes you feel better:
var match = Regex.Match(myInput, String.Format( "^{0}$", myPattern ) );
Or you may even be able to do this:
myPattern = "^" + myPattern + "$";
var match = Regex.Match(myInput, myPattern );
But as mentioned, it's just semantics. As long as your code is clear, it shouldn't be a problem when it comes to readability.
精彩评论