regex - Splitting a string into words and punctuation with Ruby -
i'm working in ruby , want split string , punctuation array, want consider apostrophes , hyphens parts of words. example,
s = "here...is happy-go-lucky string i'm writing"
should become
["here", "...", "is", "a", "happy-go-lucky", "string", "that", "i'm", "writing"].
the closest i've gotten still inadequate because doesn't consider hyphens , apostrophes part of word.
this closest i've gotten far:
s.scan(/\w+|\w+/).select {|x| x.match(/\s/)}
which yields
["here", "...", "is", "a", "happy", "-", "go", "-", "lucky", "string", "that", "i", "'", "m", "writing"]
.
you can try following:
s.scan(/[\w'-]+|[[:punct:]]+/) #=> ["here", "...", "is", "a", "happy-go-lucky", "string", "that", "i'm", "writing"]
Comments
Post a Comment