【问题标题】:Parsing Text with Python RegEx re.findall使用 Python RegEx re.findall 解析文本
【发布时间】:2016-04-28 20:25:39
【问题描述】:

我有一个长字符串需要分组解析,但需要更多控制。

import re

RAW_Data = "Name Multiple Words Testing With 1234 Numbers and this stuff* ((Bla Bla Bla (Bla Bla) A40 & A41)) Name Multiple Words Testing With 3456 Numbers and this stuff2* ((Bla Bla Bla (Bla Bla) A42 & A43)) Name Multiple Words Testing With 78910 Numbers and this stuff3* ((Bla Bla Bla (Bla Bla) A44 & A45)) Name Multiple Words Testing With 1234 Numbers and this stuff4* ((Bla Bla Bla (Bla Bla) A46 & A47)) Name Multiple Words Testing With 1234 Numbers and this stuff5* ((Bla Bla Bla (Bla Bla) A48 & A49)) Name Multiple Words Testing With 1234 Numbers and this stuff6* ((Bla Bla Bla (Bla Bla) A50 & A51)) Name Multiple Words Testing With 1234 Numbers and this stuff7* ((Bla Bla Bla (Bla Bla) A52 & A53)) Name Multiple Words Testing With 1234 Numbers and this stuff8* ((Bla Bla Bla (Bla Bla) A54 & A55)) Name Multiple Words Testing With 1234 Numbers and this stuff9* ((Bla Bla Bla (Bla Bla) A56 & A57)) Name Multiple Words Testing With 1234 Numbers and this stuff10* ((Bla Bla Bla (Bla Bla) A58 & A59)) Name Multiple Words Testing With 1234 Numbers and this stuff11* ((Bla Bla Bla (Bla Bla) A60 & A61)) Name Multiple Words Testing With 1234 Numbers and this stuff12* ((Bla Bla Bla (Bla Bla) A62 & A63)) Name Multiple Words Testing With 1234 Numbers and this stuff13* ((Bla Bla Bla (Bla Bla) A64 & A65)) Name Multiple Words Testing With 1234 Numbers and this stuff14* ((Bla Bla Bla (Bla Bla) A66 & A67)) Name Multiple Words Testing With 1234 Numbers and this stuff15* ((Bla Bla Bla (Bla Bla) A68 & A69)) Name Multiple Words Testing With 1234 Numbers and this stuff16*"

fromnode = re.findall('(.*?)(?=\*\s)', RAW_Data)

print fromnode

del fromnode
del RAW_Data

结果是:'Name Multiple Words Testing With 1234 Numbers and this stuff', '', ' ((Bla Bla Bla (Bla Bla) A40 & A41)) Name Multiple Words Testing With 3456 Numbers and this stuff2' ........等等。

我似乎无法仅捕获诸如“使用 3456 个数字和这些东西命名多个单词测试”之类的字符串,而忽略所有诸如“((Bla Bla Bla (Bla Bla) A40 & A41))”之类的字符串。任何帮助将不胜感激。

【问题讨论】:

  • Bla... 的内容是否总是在括号中并且Name Mul... 的措辞总是相同?
  • 你只想要括号外的东西吗?
  • 是的,Bla Bla Bla 的东西总是在双括号内构成。那里还有一组单括号。我使用另一个 re.findall(('(((.*?))', RAW_Data) 来捕获这些部分。现在我想忽略它们。Name Mul.... 不过不会总是一样的。我在那里转储了一些文本,以包含多个单词、空格和数字,以作为一个包罗万象的内容。
  • @Laurel 是的,我几乎只想要括号之外的东西。类似于“用 1234 个数字和这些东西命名多个单词测试”、用 1234 个数字和这个东西命名多个单词测试”等所有组...
  • 你不能只替换括号中的所有内容,为什么?

标签: python regex findall


【解决方案1】:

你可以分开

r'\*\s*\({2}.*?\){2}\s*'

模式 (see demo) 匹配:

  • \* - 字面星号
  • \s* - 零个或多个空格
  • \({2} - 正好 2 个左括号
  • .*? - 除换行符之外的零个或多个字符(注意:如果需要跨多行匹配,请添加 re.S 标志)尽可能少到第一行
  • \){2} - 双右括号
  • \s* - 0+ 个空格。

另外:same, but unrolled (thus, a bit more efficient) regex:

\*\s*\({2}[^)]*(?:\)(?!\))[^)]*)*\){2}\s*

见IDEONE demo:

import re
p = re.compile(r'\*\s*\({2}.*?\){2}\s*')
test_str = "Name Multiple Words Testing With 1234 Numbers and this stuff* ((Bla Bla Bla (Bla Bla) A40 & A41)) Name Multiple Words Testing With 3456 Numbers and this stuff2* ((Bla Bla Bla (Bla Bla) A42 & A43)) Name Multiple Words Testing With 78910 Numbers and this stuff3* ((Bla Bla Bla (Bla Bla) A44 & A45)) Name Multiple Words Testing With 1234 Numbers and this stuff4* ((Bla Bla Bla (Bla Bla) A46 & A47)) Name Multiple Words Testing With 1234 Numbers and this stuff5* ((Bla Bla Bla (Bla Bla) A48 & A49)) Name Multiple Words Testing With 1234 Numbers and this stuff6* ((Bla Bla Bla (Bla Bla) A50 & A51)) Name Multiple Words Testing With 1234 Numbers and this stuff7* ((Bla Bla Bla (Bla Bla) A52 & A53)) Name Multiple Words Testing With 1234 Numbers and this stuff8* ((Bla Bla Bla (Bla Bla) A54 & A55)) Name Multiple Words Testing With 1234 Numbers and this stuff9* ((Bla Bla Bla (Bla Bla) A56 & A57)) Name Multiple Words Testing With 1234 Numbers and this stuff10* ((Bla Bla Bla (Bla Bla) A58 & A59)) Name Multiple Words Testing With 1234 Numbers and this stuff11* ((Bla Bla Bla (Bla Bla) A60 & A61)) Name Multiple Words Testing With 1234 Numbers and this stuff12* ((Bla Bla Bla (Bla Bla) A62 & A63)) Name Multiple Words Testing With 1234 Numbers and this stuff13* ((Bla Bla Bla (Bla Bla) A64 & A65)) Name Multiple Words Testing With 1234 Numbers and this stuff14* ((Bla Bla Bla (Bla Bla) A66 & A67)) Name Multiple Words Testing With 1234 Numbers and this stuff15* ((Bla Bla Bla (Bla Bla) A68 & A69)) Name Multiple Words Testing With 1234 Numbers and this stuff16*"
print(re.split(p, test_str))

更新

用于re.findall 的正则表达式:

(?:\*\s*\(\([^)]*(?:\)(?!\))[^)]*)*\)\))?\s*([^*]*(?:\*(?!\s*\(\()[^*]*)*)\s*

见regex demo

被它的样子吓坏了?它只是一个更简单的(?:\*\s*\(\(.*?\)\))?\s*(.*?(?=\*\s*(?:\(\(|$))) 的展开版本。

请参阅IDEONE demo。

【讨论】:

  • 我添加了用于re.findall 的模式。现在,我想问题得到了解答。
  • 再次感谢 Wiktor。我继续运行 re.findall 建议。它也有效。我唯一注意到的是它在最后捕获了一个空格。
  • 请分享输入字符串,我会处理的。
  • 我使用了与最初发布的相同的输入字符串。不过不用担心。 re.split 解决方案给了我我需要的东西。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-05-14
  • 2016-04-05
  • 2013-06-12
  • 1970-01-01
  • 1970-01-01
  • 2015-08-25
  • 1970-01-01
相关资源
最近更新 更多