THX-1138
THX-1138

Reputation: 21730

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

?

Upvotes: 0

Views: 158

Answers (2)

Armstrongest
Armstrongest

Reputation: 15409

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.

Upvotes: 0

JSBձոգչ
JSBձոգչ

Reputation: 41378

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.

Upvotes: 7

Related Questions