Using a ruby regular expression, how do I match all words in a coma separated list, but only match if the entire word contains valid word characters (ie: letter number or underscore). For instance, given the string:
"see, jane, run, r#un, j@ne, r!n"
I would like to match the words
'see', 'jane' and 'run',
but not the words
'r#un', 'j@ne' or 'r1n'.
I do not want to match the coma ... just the words themselves.
I have started the regex here: http://rubular.com/regexes/12126
s="see, jane, run, r#un, j@ne, r!n, fast"
s.scan(/(?:\A|,\s*)(\w+)(?=,|\Z)/).flatten
# => ["see", "jane", "run", "fast"]
其他方式
result = s.split(/[\\s,]/).select{|_w| _w =~ /^\\w+$/}
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.