开发者

How do I use a regular expression to match strings that don't start with an empty space?

开发者 https://www.devze.com 2022-12-21 12:46 出处:网络
I want a regular expression 开发者_运维百科that checks that a string doesn\'t start with an empty space.

I want a regular expression 开发者_运维百科that checks that a string doesn't start with an empty space.

I want to do something like this:

Is the following ValidationExpression right for it?

string ValidationExpression = @"/^[^ ]/";

if (!String.IsNullOrEmpty(GroupName) && !Regex.IsMatch(GroupName, ValidationExpression))
{    
}


How about "^\S" This will make sure that the first character is not a whitespace character.


You can also use:

   if(GroupName.StartsWith(string.Empty)); // where GroupName == any string


Regex rx = new Regex(@"^\s+");

You can check with

  Match m1 = rx.Match("   ");  //m1.Success should be true
  Match m2 = rx.Match("qwerty   ");  //m2.Success should be false


Something like this, maybe :

/^[^ ]/

And, for a couple of notes about that :

  • The first ^ means "string starts with"
  • The [^ ] means "one character that is not a space"
  • And the // are regex delimiter -- not sure if they are required in C#, though.
0

精彩评论

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