Jimmy
Jimmy

Reputation: 23

REGEX - Check always start with "/"

In ASP.NET Validator, I want to validate a string like this - "/script/www/test/123.45" Basically, ensure that the string should always start with "/" followed by anything. If it does not start with "/" then its an error condition.

Upvotes: 0

Views: 198

Answers (3)

Jakob
Jakob

Reputation: 24370

The answers before me are great, just adding yet another extremely simple one:

if (path[0] == '/')

Using a regular expression for this would be like using power tools instead of cutlery when dining.

Upvotes: 2

El Ronnoco
El Ronnoco

Reputation: 11912

bool isValid(string sInputString)
{
  Regex rex = new Regex(@"^/");
  return rex.IsMatch(sInputString);
}

Upvotes: 1

spender
spender

Reputation: 120518

if( !path.StartsWith("/") ) throw...

?

Regex seems like overkill.

Upvotes: 4

Related Questions