python regex for repeating string

后端 未结 4 1530
南方客
南方客 2021-01-07 04:31

I am wanting to verify and then parse this string (in quotes):

string = \"start: c12354, c3456, 34526; other stuff that I don\'t care about\"
//Note that som         


        
4条回答
  •  渐次进展
    2021-01-07 04:59

    This can be done (pretty elegantly) with a tool like Pyparsing:

    from pyparsing import Group, Literal, Optional, Word
    import string
    
    code = Group(Optional(Literal("c"), default='') + Word(string.digits) + Optional(Literal(","), default=''))
    parser = Literal("start:") + OneOrMore(code) + Literal(";")
    # Read lines from file:
    with open('lines.txt', 'r') as f:
        for line in f:
            try:
                result = parser.parseString(line)
                codes = [c[1] for c in result[1:-1]]
                # Do something with teh codez...
            except ParseException exc:
                # Oh noes: string doesn't match!
                continue
    

    Cleaner than a regular expression, returns a list of codes (no need to string.split), and ignores any extra characters in the line, just like your example.

提交回复
热议问题