Re: Quote-aware string splitting

2005-04-26 Thread Jeffrey Froman
Bengt Richter wrote: > Oops, note some spaces inside quotes near ss and missing double quotes in > result. And here I thought the main problem with my answer was that it didn't split unquoted segments into separate words at all! Clearly I missed the generalization being sought, and a more robust

Re: Quote-aware string splitting

2005-04-26 Thread Paul McGuire
Quoted strings are surprisingly stateful, so that using a parser isn't totally out of line. Here is a pyparsing example with some added test cases. Pyparsing's quotedString built-in handles single or double quotes (if you don't want to be this permissive, there are also sglQuotedString and dblQuo

Re: Quote-aware string splitting

2005-04-26 Thread Bengt Richter
On Mon, 25 Apr 2005 19:40:44 -0700, Jeffrey Froman <[EMAIL PROTECTED]> wrote: >J. W. McCall wrote: > >> For example, given the string: >> >> 'spam "the life of brian" 42' >> >> I'd want it to return: >> >> ['spam', 'the life of brian', '42'] > >The .split() method of strings can take a substrin

Re: Quote-aware string splitting

2005-04-25 Thread George Sakkis
> import re > regex = re.compile(r''' >'.*?' | # single quoted substring >".*?" | # double quoted substring >\S+ # all the rest >''', re.VERBOSE) Oh, and if your strings may span more than one line, replace re.V

Re: Quote-aware string splitting

2005-04-25 Thread Jeffrey Froman
J. W. McCall wrote: > For example, given the string: > > 'spam "the life of brian" 42' > > I'd want it to return: > > ['spam', 'the life of brian', '42'] The .split() method of strings can take a substring, such as a quotation mark, as a delimiter. So a simple solution is: >>> x = 'spam "the

Re: Quote-aware string splitting

2005-04-25 Thread George Sakkis
> "J. W. McCall" <[EMAIL PROTECTED]> writes: > > > > I need to split a string as per string.strip(), but with a > > modification: I want it to recognize quoted strings and return them as > > one list item, regardless of any whitespace within the quoted string. > > > > For example, given the string:

RE: Quote-aware string splitting

2005-04-25 Thread Tony Meyer
> I need to split a string as per string.strip(), but with a > modification: > I want it to recognize quoted strings and return them as one > list item, > regardless of any whitespace within the quoted string. See the recent python-tutor thread starting here:

Re: Quote-aware string splitting

2005-04-25 Thread Tim Heaney
"J. W. McCall" <[EMAIL PROTECTED]> writes: > > I need to split a string as per string.strip(), but with a > modification: I want it to recognize quoted strings and return them as > one list item, regardless of any whitespace within the quoted string. > > For example, given the string: > > 'spam "th

Quote-aware string splitting

2005-04-25 Thread J. W. McCall
Hello, I need to split a string as per string.strip(), but with a modification: I want it to recognize quoted strings and return them as one list item, regardless of any whitespace within the quoted string. For example, given the string: 'spam "the life of brian" 42' I'd want it to return: ['spa