python - 使用 PyParsing 匹配 2 个以上空格

标签 python regex pattern-matching match pyparsing

我有一个如下所示的字符串:

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)

enter image description here

示例

Live Demo

示例文本

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/

相关文章:

Python:如何模拟 SQLAlchemy 事件处理程序(使用 mock.unittest)

javascript - 如何检查字符串 "should not contain leading, trailing, and mutliple commas in middle"

matlab - 提取不规则物体的垂直和水平尺寸

regex - makefile 目标上的多个词干

javascript - 在句子中,如何使第一个字母大写,后跟点 ('.' )和空格(零个或多个)

javascript - 如何去掉字符串的某些部分并将主题放入数组中?

regex - guid 的 vim 模式是什么

python - 出现空白图像

python - 需要一个以微秒为十进制的时间戳

Python TextBlob 包 - 确定 '%' 符号的 POS 标记,但不将其打印为单词