[Tutor] Regular Expression help - parsing AppleScript Lists as Strings

Kent Johnson kent37 at tds.net
Thu Nov 1 13:32:24 CET 2007


Andrew Wu wrote:

>    pattern3 = '''
>       ^{
>       (
>       %s
>       | {%s}   # Possible to have 1 level of nested lists
>       ,?)*     # Items are comma-delimited, except for the last item
>       }$
>    ''' % (pattern2, pattern2)

The above doesn't allow comma after the first instance of pattern2 and 
it doesn't allow space after either instance. Here is a version that 
passes your tests:

    pattern3 = '''
       ^{
       (
       (%s
       | {%s})   # Possible to have 1 level of nested lists
       ,?\s*)*     # Items are comma-delimited, except for the last item
       }$
    ''' % (pattern2, pattern2)

You might want to look at doing this with pyparsing, I think it will 
make it easier to get the data out vs just recognizing the correct pattern.

Kent

PS Please post in plain text, not HTML.


More information about the Tutor mailing list