From: Massimiliano Mirra Date: 2002-02-04T03:28:48+09:00 Subject: bash-like command splitting (for regexp wizards) I am trying to split a command line containing several commands that are concatenated bash-style, i.e. with `||', `&&' and `;' as separators. Such separator characters need also be preserved. I'm using the following code: dividers = ['\|\|', '&&', ';'] divs = dividers.collect {|div| "(#{div})"}.join('|') SPLIT_RE = /([^#{divs}]+)|#{divs}/ 'ls || cd dir; grep word * && beep'.scan(SPLIT_RE).flatten.compact It correctly splits the string in the example into: ["ls ", "||", " cd dir", ";", " grep word * ", "&&", " beep"] However, I'd still like those character sequences to be non-splitting when they are part of a single or double quote enclosed string. The code above will choke (of course) when given a string like: 'ls || cd dir; grep "word; sentence" && beep' It will produce: ["ls ", "||", " cd dir", ";", " grep \"word", ";", " sentence\" ", "&&", " beep"] Instead of: ["ls ", "||", " cd dir", ";", " grep \"word; sentence\" ", "&&", " beep"] I've been scratching my head for a while but I'm afraid I've hit the bottom of my skills with regular expressions. Any wizard out there has a recipe for this? Massimiliano