Trying to split a string into separate words or “quoted words” and want to keep quotes in the resulting array

I am trying to split a type string Presentation about "Test Driven Development"into an array like this:

[ 'Presentation',
  'about',
  '"Behavior Driven Development"' ]

I tried CSV::parse_line(string, col_sep: ' '), but it leads to

[ 'Presentation',
  'about',
  'Behavior Driven Development' ] # I'm missing the quotes here

I also tried some regex magic, but I am still a newbie and not successful. I think this is pretty easy for a professional, so maybe someone can point me in the right direction? Thank.

+5
source share
3 answers

You can use the following regular expression split:

str = 'Presentation about "Test Driven Development"'
p str.split(/\s(?=(?:[^"]|"[^"]*")*$)/)
# => ["Presentation", "about", "\"Test Driven Development\""]

, , , , ". , , .

scan ( ):

p str.scan(/(?:\w|"[^"]*")+/)
# => ["Presentation", "about", "\"Test Driven Development\""]
+13

Howard, :

class String
  def tokenize
    self.
      split(/\s(?=(?:[^'"]|'[^']*'|"[^"]*")*$)/).
      select {|s| not s.empty? }.
      map {|s| s.gsub(/(^ +)|( +$)|(^["']+)|(["']+$)/,'')}
  end
end

:

> 'Presentation      about "Test Driven Development"  '.tokenize
=> ["Presentation", "about", "Test Driven Development"]
+2

Here:

"Presentation about \"Test Driven Development\"".scan(/\s?\w+\s?|"[\w\s]*"/).map {|s| s.strip}
0
source

All Articles