首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Regex编译Python段落中制表符/缩进之间的所有文本

Regex编译Python段落中制表符/缩进之间的所有文本
EN

Stack Overflow用户
提问于 2021-05-20 05:49:48
回答 1查看 168关注 0票数 0

事先道歉,如果这是在其他地方,但我一直在寻找,我不擅长regex。我使用regex从包含段落的word文档中编译句子。我需要在两个缩进之间得到具体的文本,或者如果有人能帮我找出我的当前正则表达式(稍后会显示),那么这也是有效的。例如,从以下案文;

下面是作为纯文本的图像,尽管我无法获得相同的格式:

  1. 一种方法,包括: 在第一区域中存储与运输的操作方式有关的第一数据; 在第二区域中存储与运输的操作方式有关的第二数据;其中,所述第一和第二数据基于运输操作通过所述第一和第二区域时的综合能耗效率;以及 基于综合能源消耗效率对运输功能进行修改。
  2. 索赔1的方法,包括修改运输的功能,使其在符合一个或多个社会必需品和车辆法律的情况下,以最大的综合效率、消费效率运作。

下面是从我的函数中读到的文本:

  1. 一种方法,包括:在第一地区存储与运输的操作方式相关的第一转机数据;在第二地区存储与运输的操作方式有关的第二数据;其中,第一和第二数据基于在第一和第二区域的运输机动时的联合能耗效率;其次,在综合能耗效率的基础上,给出了较好的改造功能

当我从.docx文件中打印读进来的文本时,所有这些都被输出到一行中。

我需要提取以下几行:

在第一区域中存储与运输的操作方式有关的第一数据; 在第二区域中存储与运输的操作方式有关的第二数据;其中,所述第一和第二数据基于运输操作通过所述第一和第二区域时的综合能耗效率;以及 基于综合能源消耗效率对运输功能进行修改。

我当前的regex模式如下:

代码语言:javascript
复制
pattern = re.compile(r"[ \t]+([^\s.;]+\s*)+[.;]+")

如前所述,如果有人能帮我解出这个正则表达式,让我读到分号或句点,那就太好了,否则,我就会明白,我的一部分问题是,我有\t而不是仅仅\t,但是当我删除这个空格时,我就没有输出了。另外,当前的正则表达式应该读到分号,但是我将读到下一个缩进,这样我就可以在后面解析句子并删除不必要的信息。如果有帮助的话,我的当前输出如下所示:

这里只是输出的原始文本:

一种方法,包括:在第一存储与第一和第二数据中的运输操作方式有关的第二数据中存储与运输的操作方式相关的第一转接数据的第一转接器,其基于通过第一和第二数据的运输机动时的组合能源消耗效率,以及基于要求1的组合能耗方法来修改运输的组合能量消耗效率,以在符合一个或多个社会需求和车辆需求的同时,以最大的组合效率运行的组合能量消耗效率。

图像中的每一行文本都是我的代码中的一个输出。从.docx的原始摘录中无法识别的任何文本都只是.docx文件中的文本。

最后,下面是我目前正在使用的代码:

代码语言:javascript
复制
def find_matches(text):
    print(text)
    pattern = re.compile(r"[ \t]+([^\s.;]+\s*)+[.;]+")
    return capitalize([m.group() for m in re.finditer(pattern, text)])


for match in find_matches(text=docText):
    ct += 1
    match_words = match.split(" ")
    match = " ".join(match_words[:-1])
    print(match)

所以,我所需要的只是阅读一些regex,意图缩进,再一次,抱歉,如果这是在其他地方,我就是找不到。

我添加这一点,因为我终于得到了一个正则表达式模式的一些输出,然而,它似乎都是胡说八道,我认为这是因为编码。下面是我要展示的代码:

代码语言:javascript
复制
doc = open('P.docx', mode='r', encoding = "ISO-8859-1")
docText = doc.read()
pattern = r"^[^.;]*\s{2,}([^\s.;]*(?:\s+[^\s.;]+)+[.;])"
print(re.findall(pattern, docText, re.MULTILINE))

这只是一些(因为有很多)输出,我从使用这个:

'½ú\x04Ü\x13\x8eÕ\nõ+;','\x7fîÙ(\x11\x90\x85íÆ\x83Bs\x15Ü\xa0g\x03i\x00a\x070§¬gÃo\x18Ë\x9a\x81i[¡\x8eÃ{\x96FÃ9\x9f\x8aãð6°AÏ>ö·\x98+\x80e·!f\x8d\x0e{\x12W\x1eéÝ}iûͨ½niü>Ú¶mB¥»\tÜÀªÓÿº$í}b^3¢¡7\t\x1amwR\x19ò\x96\x83"Hf\x0fòÑ«NÀ=áXÝP½²£ç\x1a\x01ZÁÍEÃÌ4ÒÄ\x90-dÌìáy½Þ|yFÕ,4ýÂÍ.',"ð`\x9c\n\x99´-Á:bÒÒY²O\x86\x88\x06'\x93°Îx4û§'?Ì÷\xad\x00m{N¸r6a\x86×8Û\x9drâúÙÄ9\x85\x91\x0c-;",

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-05-20 09:51:48

您可以使用一个或多个空格或制表符开始匹配,并捕获组中所需的内容。

代码语言:javascript
复制
^[ \t]+([^\s.;]+(?:\s+[^\s.;]+)*[.;])
  • 字符串的^开始
  • [ \t]+匹配1+制表符或空格
  • ( Capture group 1
    • [^\s.;]+匹配除.;以外的1+非空格字符
    • (?:\s+[^\s.;]+)*可选择重复匹配1+空白字符和1+非空白字符( .;除外)
    • [.;]匹配.;

  • )闭组1

Regex演示 x- Python演示

示例

代码语言:javascript
复制
import re
from pprint import pprint
pattern = r"^[ \t]{2,}([^\s.;]+(?:\s+[^\s.;]+)+[.;])"

s = ("1. A method, comprising:\n\n"
     "      storing a first data related to an operation style of a transport in a first area;\n\n"
     "     storing a second data related to an operation style of the transport in a second area; wherein the first and second data is based on a combined energy consumption efficiency as the transport maneuvers through the first and second area; and \n\n"
     "     modifying functionality of the transport based on the combined energy consumption efficiency.\n\n"
     "2. The method of claim 1, comprising modifying functionality of the transport to operate at a greatest combined efficiency consumption efficiency while in compliance with one or more of social necessities and vehicular laws. \n\n"
     "And here is the text that is actually read in from my function:\n\n"
     "> 1. A method, comprising:            storing a first data related to an operation style of a transport in a first area;             storing a second data related to an operation style of the transport in a second area; wherein the first and second data is based on a combined energy consumption efficiency as the transport maneuvers through the first and second area; and             modifying functionality of the transport based on the combined energy consumption efficiency.2.     The method of claim 1, comprising modifying functionality of the transport to operate at a greatest combined efficiency consumption efficiency while in compliance with one or more of social necessities and vehicular laws.\n")

result = re.findall(pattern, s, re.MULTILINE)
pprint(result, width=100)

输出

代码语言:javascript
复制
['storing a first data related to an operation style of a transport in a first area;',
 'storing a second data related to an operation style of the transport in a second area;',
 'modifying functionality of the transport based on the combined energy consumption efficiency.']
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67614647

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档