开发者

.NET Entire line match

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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜