reg. expression

  • Thread starter Thread starter Jeroen Ceuppens
  • Start date Start date
J

Jeroen Ceuppens

Hi,

I need a check function to check if a string is correct.

what is possible for string:

every number and letter,
but : the string cannot start with a number
cannot contain spaces
cannot contain *,/, ....

If there is a value in the string that is not possible it must be replaced
by the underscore _

How should I do that?

Thx
Jeroen
 
Jeroen Ceuppens said:
I need a check function to check if a string is correct.

what is possible for string:

every number and letter,
but : the string cannot start with a number
cannot contain spaces
cannot contain *,/, ....

If there is a value in the string that is not possible it must be replaced
by the underscore _

How should I do that?

I personally wouldn't bother with a regular expression for this - just
hand code it:

static readonly char[] DisallowedChars = " */".ToCharArray();

static bool IsStringCorrect (string value)
{
// Insert logic for null here. Could throw an exception,
// return true or false, depending on your needs.

if (value.Length==0)
{
return true;
}

if (Char.IsDigit(value, 0))
{
return false;
}

return (value.IndexOfAny(DisallowedChars)==-1);
}
 
Back
Top