in reply to Regex for escaping spaces in strings when there are quotes

So you want to split 'ABC DEF "GHI JKL" MNO' into the list ('ABC', 'DEF', '"GHI JKL", 'MNO')? If so, here's a Perl regex:
@parts = $string =~ m{"[^"]*"|\S+}g;
If you then want to remove the quotes from the fields with them, that's your job.

_____________________________________________________
Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

Replies are listed 'Best First'.
Re: Re: Regex for escaping spaces in strings when there are quotes
by Incognito (Pilgrim) on Nov 29, 2001 at 02:47 UTC
    Kudos to you!!! You get my ++ any day... This is exactly the solution we've needed, and it was such a simple regex... I don't know why I didn't think of it.

    For anyone that's interested, here's the new JavaScript code:

    <SCRIPT LANGUAGE="JavaScript"> function ParseStringRegExp (strSource) { // Split the modified string by spaces or quoted strings. var aryOutput = strSource.match (/"[^"]*"|\S+/g); return (aryOutput); } </SCRIPT>
    No extra arrays needed, and the function is basically a one liner... The VB implementation is just as simple, a single Regex, but of course, we have to iterate through the Matches collection and stored each value into an array for the return value. If anyone wants to see how ugly Regexes are in VBScript, here you are:
    <% Function ParseStringRegExp (strSource) Dim objRegex Dim colMatches Dim objMatch Set objRegex = New RegExp ' Create a regular expression +object. objRegex.Pattern = """[^""]*""|\S+" ' Set pattern. objRegex.Global = True ' Set global applicabi +lity. Set colMatches = objRegex.Execute(strSource) ' Execute search. Dim aryOutput() ReDim aryOutput(colMatches.Count - 1) ' Prepare the output a +rray. Dim lngMatchCounter ' Grab the colMatches collection and store each value into an array. lngMatchCounter = 0 For Each objMatch In colMatches aryOutput(lngMatchCounter) = objMatch.Value lngMatchCounter = lngMatchCounter + 1 Next ParseStringRegExp = aryOutput End Function %>