qsplit.py

poo/qsplit.py · 2.2 KB

From the archive poo.tar.gz

Download the archive   « Back to the archive contents

import regex

r=regex.compile('[ \t\n]*'              # Skip leading whitespace
                '\('                    # Two alternative token
                                        # styles
                '\([^ \n\t"\'][^ \t\n]*\)'# A non-quotemark or WS
                                        # character, up to the next
                                        # whitespace
                  '\|'                  #   or
                '\("[^"]*"\)'           # A double quote, followed
                                        # by non-quotes, ending with
                                       # another double quote
                  '\|'                  #   or
                "\('[^']*'\)"           # A single quote, followed
                                        # by non-quotes, ending with
                                        # another single quote
                '\)'                    # End of the alternatives
               )

rl=regex.compile('[ \t\n]*'              # Skip leading whitespace
                '\('                    # Two alternative token
                                        # styles
                '\([^ \n\t"\'][^,]*\)'  # A non-quotemark or WS
                                        # character, up to the next
                                        # comma
                  '\|'                  #   or
                '\("[^"]*"\)'           # A double quote, followed
                                        # by non-quotes, ending with
                                        # another double quote
                  '\|'                  #   or
                "\('[^']*'\)"           # A single quote, followed
                                        # by non-quotes, ending with
                                        # another single quote
                '\)'                    # End of the alternatives
               )


def qsplit(s):
    lst=[] ; pos=0
    while (s!=""):
        res=r.match(s, pos)
        if res==-1: break
        lst.append(r.group(1))
        pos=r.regs[0][1]
    return lst

def qsplit_list(s):
    lst=[] ; pos=0
    while (s!="" and pos < len(s)):
        res=rl.match(s, pos)
        if res==-1: break
        lst.append(rl.group(1))
        pos=rl.regs[0][1]+1
    return lst

Site Stats

Snippets: 1791
Codebases: 571