user336635
user336635

Reputation: 2301

Regex doesn't match

Why this regex:

[^\s]+  

...says that this string:

"user's extension"

isn't exact match?

Upvotes: 2

Views: 568

Answers (2)

Eser Aygün
Eser Aygün

Reputation: 8014

Your regex would match strings only with one or more non-whitespace characters. "user's extension" contains matching substrings, but it is not a match itself because of the space character.

Upvotes: 2

Tim Pietzcker
Tim Pietzcker

Reputation: 336508

The regex only matches a string that doesn't contain any whitespace. Your matching method appears to apply the regex to the entire string, therefore it fails.

[abc] is a character class, meaning "either a, b or c".
[^abc] is the inverse of that class meaning "any character except a, b or c".
\s means "any whitespace character".
[^\s] (which can also be written as \S) means "any non-whitespace character".
+ means "one or more of the preceding token.

Upvotes: 7

Related Questions