tags:

views:

55

answers:

2

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 = Regex.Match(myInput, "^" + myPattern + "$");

?

+7  A: 

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.

JSBangs
A: 

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.

Atømix