What is the regular expression for matching that contains no white space in between text?
Asked Answered
D

3

6

I am looking for a regular expression for matching that contains no white space in between text but it may or may not have white space at start or end of text.

Dehydrate answered 11/6, 2010 at 6:57 Comment(0)
B
18

You want something like this: (see it in action on rubular.com):

^\s*\S+\s*$

Explanation:

  • ^ is the beginning of the string anchor
  • $ is the end of the string anchor
  • \s is the character class for whitespace
  • \S is the negation of \s (note the upper and lower case difference)
  • * is "zero-or-more" repetition
  • + is "one-or-more" repetition

References


Can the "text" part be empty?

The above pattern does NOT match, say, an empty string. The original specification isn't very clear if this is the intended behavior, but if an empty "text" is allowed, then simply use \S* instead, i.e. match zero-or-more (instead of one-or-more) repetition of \S.

Thus, this pattern (same as above except * is used instead of +)

^\s*\S*\s*$

will match:

  • The empty string (i.e. the string whose length is 0)
  • Non-empty strings consisting of nothing but whitespace characters

What counts as "text" characters?

The above patterns use \S to define the "text" characters, i.e. anything but whitespace. This includes things like punctuations and symbols, i.e. the string " #@^$^* " matches both patterns. It's not clear if this is the desired behavior, i.e. it's possible that " ==== AWESOMENESS ==== " is a desired match

The pattern still works even for this case, we simply need to be more specific with our character class definitions.

For example, this pattern:

/^[^a-z]*[a-z]*[^a-z]*$/i

Will match (as seen on rubular.com):

 ==== AWESOMENESS ====

But not:

 ==== NOT AWESOME ==== 

Note that the ^ metacharacter, when used as the first character in a character class definition, no longer means the beginning of the string anchor, but rather a negation of the character class definition.

Note also the use of the /i modifier in the pattern: this enables case insensitive matching. The actual syntax may vary between languages/flavors.

References

Bubalo answered 11/6, 2010 at 7:3 Comment(3)
+1, but see the comment to Taz's own answer - the empty string should be matched, too.Bethesde
@Tim: addressed, among other issues.Bubalo
Hi! I'm trying to use \S as you have it here to prevent spaces in my string check, however no long. Would you mind a look here? #43764525Stigmatic
D
2

Just found answer myself. So I am writing it here so that It may be helpful for someone else.
^\s*\S*\s*$

Dehydrate answered 11/6, 2010 at 7:0 Comment(5)
This only matches if the "text" is only one character, and it must be followed by a whitespace character. From the (rather vague) specification, I'd imagine you want to use * and + for all 3 parts.Bubalo
Sorry * were there. I think stackoverflow does not display them (?). So corrected.Dehydrate
This also matches the empty string. Do you want that? Also, the brackets around \S are superfluous.Bethesde
SO interprets * as indicators for italic and ** for bold text. That's one of the reasons you need to surround code samples with backticks (as you did) or indent them by 4 spaces.Bethesde
@Tim Yes empty strings are allowed in my case. And Removed the brackets :)Dehydrate
C
1

This should do it :

^\s*\S+\s*$
  • " aaadadadad " => matches
  • " aaa aaaaa " => doesn't match
Carlsen answered 11/6, 2010 at 7:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.