Splitting on whitespace, period, comma or double quotes, and not on single quotes:
str = %Q{this is the.string to's split,real "ok" nice-like.}
str.split(/\s|\.|,|"/)
=> ["this", "is", "the", "string", "", "", "", "to's", "split", "real", "", "ok", "", "nice-like"]
How to eloquently remove empty strings?
How to eloquently remove strings that are shorter than MIN_LENGTH?
The idea of using split
is not right in this case. You should be using scan
.
str = %Q{this is the.string to's split,real "ok" nice-like.}
str.scan(/[\w'-]+/)
# => ["this", "is", "the", "string", "to's", "split", "real", "ok", "nice-like"]
In order to match strings that are MIN_LENGTH
or longer, do like this:
MIN_LENGTH = 3
str.scan(/[\w'-]{#{MIN_LENGTH},}/)
# => ["this", "the", "string", "to's", "split", "real", "nice-like"]
When to use split, when to use scan
scan
.split
.scan
.split
.I'm not entirely clear on the problem domain, but if you just want to avoid the empty strings, why not split on one or more occurrences of your separators?
str.split /[\s\.,"]+/
I would think a simple way to do that is as follows:
str.split(/\s|\.|,|"/).select{|s| s.length >= MIN_LENGTH}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With