Incognito has asked for the wisdom of the Perl Monks concerning the following question:
My JavaScript (and VB) friends have brought me a question that I am having trouble determining what the best regular expression would be...
They were discussing the need to parse through a string that contained sets of search parameters separated by spaces. Sounds easy enough... "Just split on the spaces in the string and put it into an array!" we'd say.
Of course, if this was the case, he'd be doing that right now. The problem is that to allow the user to have spaces in any of these search parameters, we must allow them to quote the words with the quote character (").
The original regular expressions we designed for this used a temporary array to hold the value in the escaped quoted strings, but someone suggested just escaping all instances of a slash (\) with (\\) (within the quoted strings), and then replacing all spaces with (\s) within these quotes strings... This sounds much better...
The problem is I can't think of a good regex to look through the string and replace/escape only those spaces within quote with (\s)... Once we do this, doing a split on spaces will give us the array with all desired atoms.
SearchAtom1 "Quoted Search Atom2" SearchAtom3 "" SearchAtom5Since4Wa +sEmpty "Search Atom 6"
<SCRIPT LANGUAGE="JavaScript"> function ParseStringRegExp (strSource) { var intMatchCounter = 0; var aryStoredValues = new Array(); var strUniqueID; var strWorking = strSource; var aryQSMatch; // Iterate through each quoted string and replace with UniqueID while (aryQSMatch = strWorking.match (/"[^"]*"/)) { strUniqueID = "__" + intMatchCounter + "__"; // Generate th +e UniqueID strWorking = strWorking.replace (/"[^"]*"/, strUniqueID); // Re +place the value with UniqueID aryStoredValues[intMatchCounter++] = aryQSMatch[0]; // Store +removed value into array } // Split the modified string by spaces var aryOutput = strWorking.split (/\s+/); // Go through array and replace UniqueIDs with original values. for (i = 0; i < aryOutput.length; i++) { if (aryQSMatchResults = aryOutput[i].match(/__(\d+)__/)) { aryOutput[i] = aryStoredValues[aryQSMatchResults[1]]; // Do r +eplacement here } } return (aryOutput); } var strSource = 'SearchAtom1 "Quoted Search Atom2" SearchAtom3 "" Se +archAtom5Since4WasEmpty "Search Atom 6"'; var aryOutput = ParseStringRegExp (strSource); alert (aryOutput); </SCRIPT>
I'm sure that some of you can think of a good regex for this, but we have to keep in mind that these regexs need to be converted to the 'lesser' languages of JavaScript (and also Visual Basic - PUKE - the VB implementation of regular expressions is SO LAME), so it must be simple, rather than pretty and obfuscated :)<SCRIPT LANGUAGE="JavaScript"> function ParseStringRegExp (strSource) { var intMatchCounter = 0; var aryStoredValues = new Array(); var strUniqueID; var strWorking = strSource; var aryQSMatch; // Escape all backslashes and spaces. // *** // what's a good regex(es) we can write for this section? // *** // Split the modified string by spaces. var aryOutput = strWorking.split (/\s+/); // Go through array and replace UniqueIDs with original values. for (i = 0; i < aryOutput.length; i++) { // *** // Do the unescaping back to regular slashes and spaces for each // array value here for aryOutput[i]. // *** } return (aryOutput); } var strSource = 'SearchAtom1 "Quoted Search Atom2" SearchAtom3 "" Se +archAtom5Since4WasEmpty "Search Atom 6"'; var aryOutput = ParseStringRegExp (strSource); alert (aryOutput); </SCRIPT>
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Regex for escaping spaces in strings when there are quotes
by japhy (Canon) on Nov 29, 2001 at 01:09 UTC | |
by Incognito (Pilgrim) on Nov 29, 2001 at 02:47 UTC | |
|
Re: Regex for escaping spaces in strings when there are quotes
by IlyaM (Parson) on Nov 29, 2001 at 00:20 UTC | |
by Incognito (Pilgrim) on Nov 29, 2001 at 00:28 UTC | |
by IlyaM (Parson) on Nov 29, 2001 at 00:36 UTC |