Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regex for at least 2 empty space?

Tags:

regex

ruby

i need to ignore anything that is one space, and empty space at least bigger than one space should be matched...

"MARY   HAD A LITTLE            LAMB"

i expect

"MARY", "HAD A LITTLE", "LAMB"
like image 298
bohohasdhfasdf Avatar asked Jan 28 '10 01:01

bohohasdhfasdf


People also ask

How do you specify a space in regex?

The most common forms of whitespace you will use with regular expressions are the space (␣), the tab (\t), the new line (\n) and the carriage return (\r) (useful in Windows environments), and these special characters match each of their respective whitespaces.

How do you add blank space in regex?

\s is the regex character for whitespace. It matches spaces, new lines, carriage returns, and tabs.

Can regex contain space?

Yes, also your regex will match if there are just spaces.

Does \w include spaces?

\W means "non-word characters", the inverse of \w , so it will match spaces as well.


1 Answers

Whitespace matching is \s and you can supply a minimum and maximum in curly braces. You can also omit either of them, like so:

\s{2,}

So your code would be like:

"MARY   HAD A LITTLE            LAMB".split(/\s{2,}/)

You can test it online here!

like image 106
Brian McKenna Avatar answered Sep 20 '22 10:09

Brian McKenna