我有一个如下所示的字符串:
date Not Important value NotImportant2
11.11.13 useless . useless,21 useless 2 14.21 asmdakldm
21.12.12 fmpaosmfpoamsp 4 41 ajfa9si90
我只需提取日期和末尾的值。
如果我使用标准过程来匹配多个单词,pyparsing 会将“不重要”列的最后一个数字匹配为“值”。
anything = pp.Forward()
anything << anyword + (value | anything)
myParser = date + anything
我认为最好的方法是强制 pyparsing 匹配至少 2 个空格,但我真的不知道如何做。有什么建议吗?
最佳答案
描述
要匹配 2 个或更多空格,您可以使用 \s{2,}
这个表达式将:
- 捕获日期字段
- 捕获倒数第二个字段
^(\d{2}\.\d{2}\.\d{2})[^\r\n]*\s(\S+)\s{2,}\S+\s*(?:[\r\n]|\Z)
示例
示例文本
date Not Important value NotImportant2
11.11.13 useless . useless,21 useless 2 14.21 asmdakldm
21.12.12 fmpaosmfpoamsp 4 41 ajfa9si90
匹配
[0][0] = 11.11.13 useless . useless,21 useless 2 14.21 asmdakldm
[0][3] = 11.11.13
[0][4] = 14.21
[1][0] = 21.12.12 fmpaosmfpoamsp 4 41 ajfa9si90
[1][5] = 21.12.12
[1][6] = 41
关于python - 使用 PyParsing 匹配 2 个以上空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17922471/