简体   繁体   中英

regex that allows 5-10 characters but can have spaces in-between not counting

Problem

Build a regex statement that allows the following:

  1. minimum 5 characters
  2. maximum 10 characters
  3. can contain whitespace but whitespace does not increment character count
  4. any non-whitespace characters increment character count

Test Cases:

 expected_to_pass = ['testa', ' test a', 12342, 1.234, 'test a']
 expected_to_fail = [' test', 'test ', ' test ', '     ', 1234, 0.1, '           ','12345678901']

Example regex statements and their purpose

Allow 5-10 non-whitespace characters:

[\S]{5,10}$

Allow 5-10 characters regardless of whitespace:

[\s\S]{5,10}$

I've been farting around with this for a few hours and cannot think of the best way to handle this.

How's this?

\s*(?:[\w\.]\s*){5,10}+$

Or:

\s*(?:[\w\.]\s*){5,10}$

Also, if ANY non-whitespace character goes:

\s*(?:\S\s*){5,10}$

You can test it here

There is a wrong assumption in your question: \\w doesn't match all non-space-characters, it matches word characters - this means letters, digits and the underscore. Depending on language and flags set, this might include or exclude unicode letters and digits. There are a lot more non-space-characters, eg . and | . To match space-characters one usually uses \\s , thus \\S matches non-space-characters.

You can use ^\\s*(?:\\S\\s*){5,10}$ to check your requirements. You might be able to drop the anchors, if you use some kind of full match functionality (eg Java .matches() or Python re.fullmatch ).

Depending on the language you use, you might not want to use a regex, but iterate over the string and check character for character. This should usually be faster than regex.

Pseudocode:

number of chars = 0
for first character of string to last character of string
  if character is space
     inc number of chars by 1
return true if number of chars between 5 and 10

Check this out: (\\s*?\\w\\s*?){5,10}$

It won't match 1.234 because . is not included inside \\w set

If you need it to be included then: (\\s*?[\\w|\\.]\\s*?){5,10}$ (\\s*?[\\w\\.]\\s*?){5,10}$

Cheers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM