For example, my string is:
"abcd :e fghi :j klmn"
I want to get :e
and :j
which with special mark :
. Can I do it?
You can do this using String#scan :
"abcd :e fghi :j klmn".scan(/:\w+/)
# => [":e", ":j"]
"abcd :egg fghi :joke klmn".scan(/:\w+/)
# => [":egg", ":joke"]
"abcd :e fghi :j klmn".split(' ').select{|e| e.include? ':'}
=> [":e", ":j"]
or
"abcd :e fghi :j klmn".split(' ').select{|e| e =~ /^:\w/ }
=> [":e", ":j"]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.