Reputation: 2785
I have a requirement to remove spaces between special characters. I have a string:
abc = "test = > @ Stack overflow"
I want a string like this:
abc = "test=>@Stack overflow"
so that only spaces between, before, or after are removed. failed to create a proper regular expression or method for that. Any hint or idea?
Upvotes: 0
Views: 101
Reputation: 627468
You can use the following code:
abc = "test = > @ Stack overflow"
puts abc.gsub(/\s*([^\s\p{L}\p{N}])\s*/, "\\1")
See IDEONE demo, result: test=>@Stack overflow
.
The capturing group is necessary to restore the captured "special" character.
The [^\s\p{L}\p{N}]
stands for a non-whitespace, non-letter and non-digit character.
Upvotes: 4