ParPar
ParPar

Reputation: 7569

Regular expressions - How to prevent any space including in the first character of the string

currently I have this: @"(\S)+" , but it just prevents spaces after any full character, so how can I prevent space in the first character as well ?

Upvotes: 0

Views: 1182

Answers (1)

Alan Moore
Alan Moore

Reputation: 75272

If you want to make sure there's no whitespace in the string, use this:

^\S+$
  • ^ anchors the match to the beginning of the string.

  • \S+ matches one or more non-whitespace characters.

  • $ anchor the match to the end of the string.

If you're really using the NSRegularExpression class in Apple's Objective-C developer framework, it should look like this:

@"^\\S+$"

But if (as I suspect) you're using a C# Regex, it should look like this:

@"^\S+$"

("regex" is the general-purpose tag for regular expressions. I'll just remove that other tag.)

Upvotes: 1

Related Questions