最好使用语法或正则表达式解析 table

Parsing a table using preferably grammar or regex

有几天我卡住了;我想解析一个包含许多页面的大型文档,其中包含 table 像这样的内容:

                Col1  Col2  Col3   Col4

                TXT1       TTT     Fnam1, LNam1

                TXT2       TEE     Fnam2, Mnam LNam2

                TXT1

                TXT5       ART     Fnam3, LNam3

                     TXT6  BGT     Fnam4, LNam4

我已经用琶音编写了语法 (python) 如果所有单元格都不为空,我可以解析 table 但如果有一个或多个空单元格,一切都会成为一部分。 任何人都知道我如何在不使用歧义语法的情况下做到这一点? 提前致谢

感谢@JeffC 的提示,我通过询问空格数找到了解决方案。 这是语法:

from __future__ import print_function, unicode_literals

import os
from arpeggio import ParserPython
from arpeggio import Optional, ZeroOrMore, OneOrMore, EOF,  PTNodeVisitor, ParserPython, visit_parse_tree, Combine
from arpeggio.export import PMDOTExporter, PTDOTExporter
from arpeggio import RegExMatch as _


def wsx() : return _(r"[ \t]*")
def ws2() : return _(r"[ \t]{2}")
def ws16() : return _(r"[ \t]{16}")
def code(): return _(r"[a-zA-Z0-9]{2,}")
def col4_title(): return "Col4"
def col3_title(): return "Col3"
def col2_title(): return "Col2"
def col1_title(): return "Col1"
def cell_col4(): return [Combine(wsx,OneOrMore(word,Optional(ws2)),Optional(",", OneOrMore(ws2,word))),wsx]
def cell_col3(): return [code,wsx]
def cell_col2(): return [code,wsx]
def cell_col1(): return [code,wsx]
def team_row(): return ws16, Optional(cell_col1),Optional(ws2),Optional(cell_col2),Optional(ws2),Optional(cell_col3),Optional(ws2),Optional(cell_col4),Optional(wsx), nl
def team_table_body(): return wsx, OneOrMore(nl,team_row),
def team_table_title(): return "testtab"
def team_table_header(): return wsx, team_table_title, wsx, col1_title, wsx, col2_title, wsx, col3_title, wsx, col4_title,Optional(wsx),nl
def team_block(): return wsx, team_table_header, team_table_body