Regex: Repeated capturing groups

后端 未结 3 2288
清歌不尽
清歌不尽 2020-11-28 13:03

I have to parse some tables from an ASCII text file. Here\'s a partial sample:

QSMDRYCELL   11.00   11.10   11.00   11.00    -.90      11     11000     1.212         


        
3条回答
  •  無奈伤痛
    2020-11-28 13:20

    Unfortunately you need to repeat the (…) 8 times to get each column separately.

    ^(\S+)\s+([-.\d]+)\s+([-.\d]+)\s+([-.\d]+)\s+([-.\d]+)\s+([-.\d]+)\s+([-.\d]+)\s+([-.\d]+)\s+([-.\d]+)$
    

    If code is possible, you can first match those numeric columns as a whole

    >>> rx1 = re.compile(r'^(\S+)\s+((?:[-.\d]+\s+){7}[-.\d]+)$', re.M)
    >>> allres = rx1.findall(theAsciiText)
    

    then split the columns by spaces

    >>> [[p] + q.split() for p, q in allres]
    

提交回复
热议问题