Daniel Viglione
Daniel Viglione

Reputation: 9407

Remove non-alphabetic characters and more than one space from a string

I am not sure what I am doing wrong here:

"#New    York".gsub(/[^a-zA-Z\s]/,"").strip

This regex should remove all non-alphabetic characters and all spaces greater than 1 space.

It should give me the following result:

"New York"

What is wrong with the regex?

Upvotes: 1

Views: 313

Answers (2)

Srdjan M.
Srdjan M.

Reputation: 3405

Regex: (?:\s+(?=\s)|[^A-Za-z\s]+)

Ruby code:

"#New    York".gsub(/(?:\s+(?=\s)|[^A-Za-z\s]+)/, '')

Output:

New York

Code demo

Upvotes: 0

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626747

You can replace strip with squeeze:

"#New    York".gsub(/[^a-zA-Z\s]/,"").squeeze(" ")
# => New York

Another way is to use a regex like

"  #New \t   York  ".gsub(/\s{2,}|[^\sa-zA-Z]/, ' ').strip

Or

"  #New \t   York  ".gsub(/(\s){2,}|[^\sa-zA-Z]/, '\1').strip

Here, /\s{2,}|[^\sa-zA-Z]/ matches 2 or more consecutive whitespaces (\s{2,}) or (|) any char other than an ASCII letter or whitespace ([^\sa-zA-Z]). In case of (\s){2,}, the last whitespace captured will be inserted into the resulting string with the help of the \1 placeholder.

See a Rubular demo.

Upvotes: 1

Related Questions