大写时保留段落标记-RegEx

时间:2018-09-09 03:08:44

标签: python regex capitalization

p = re.compile(r'((?<=[\.\?!]\s)(\w+)|(^\w+))')
def cap(match):
    return(match.group().capitalize())
capitalized_1 = p.sub(cap, Inputfile)

with codecs.open('o.txt', mode="w", encoding="utf_8") as file:
  file.write(capitalized_1)

我正在使用Regex在后面大写字母。 ? !上面的代码可以做到。但是它会删除段落标记(分页窃取)并将其合并为一个大段落。

如何保留段落标记并防止结块?

输入文件:

  

在“插入”选项卡上,画廊包括旨在与文档的整体外观相协调的项目。您可以使用这些图库来插入表格,页眉,页脚,列表,封面和其他文档构造块。创建图片,图表或图表时,它们还会与您当前的文档外观保持一致。

     

通过从“主页”选项卡上的快速样式库中选择所选文本的外观,可以轻松更改文档文本中所选文本的格式。您还可以通过使用“首页”标签上的其他控件直接设置文本格式。大多数控件都提供了使用当前主题外观或直接指定格式的选择。

当前输出

  

在“插入”选项卡上,画廊包括旨在与文档的整体外观相协调的项目。您可以使用这些图库来插入表格,页眉,页脚,列表,封面和其他文档构造块。创建图片,图表或图表时,它们也会与您当前的文档外观保持一致。通过从“主页”选项卡上的快速样式库中选择所选文本的外观,可以轻松更改文档文本中所选文本的格式。您还可以使用“主页”选项卡上的其他控件直接设置文本格式。大多数控件都提供了使用当前主题外观或直接指定格式的选择。

预期输出:

  

在“插入”选项卡上,画廊包括旨在与文档的整体外观相协调的项目。您可以使用这些图库来插入表格,页眉,页脚,列表,封面和其他文档构造块。创建图片,图表或图表时,它们也会与您当前的文档外观保持一致。

     

通过从“主页”选项卡上的快速样式库中选择所选文本的外观,可以轻松更改文档文本中所选文本的格式。您还可以使用“主页”选项卡上的其他控件直接设置文本格式。大多数控件都提供使用当前主题外观或直接指定格式的选择。

编辑1:

import re,codecs
def capitalize(match):
    return ''.join([match.group(1), match.group(2).capitalize()])

with codecs.open('i.txt', encoding='utf-8') as f:
    text = f.read()

pattern = re.compile('(^|[.?!]\s+)(\w+)?')

print(pattern.sub(capitalize, text))

当我尝试根据答案1的方法从文件读取错误时抛出错误。

return ''.join([match.group(1), match.group(2).capitalize()])
AttributeError: 'NoneType' object has no attribute 'capitalize'

1 个答案:

答案 0 :(得分:1)

您可以这样做:

import re


def capitalize(match):
    return ''.join([match.group(1), match.group(2).capitalize()])

text = """on the insert tab, the galleries include items that are designed to coordinate with the overall look of your document. you can use these galleries to insert tables, headers, footers, lists, cover pages, and other document building blocks. when you create pictures, charts, or diagrams, they also coordinate with your current document look.

you can easily change the formatting of selected text in the document text by choosing a look for the selected text from the quick styles gallery on the home tab. you can also format text directly by using the other controls on the home tab. most controls offer a choice of using the look from the current theme or using a format that you specify directly."""

pattern = re.compile('(^|[.?!]\s+)(\w+)?')

print(pattern.sub(capitalize, text))

输出

On the insert tab, the galleries include items that are designed to coordinate with the overall look of your document. You can use these galleries to insert tables, headers, footers, lists, cover pages, and other document building blocks. When you create pictures, charts, or diagrams, they also coordinate with your current document look.

You can easily change the formatting of selected text in the document text by choosing a look for the selected text from the quick styles gallery on the home tab. You can also format text directly by using the other controls on the home tab. Most controls offer a choice of using the look from the current theme or using a format that you specify directly.

注释

  • (^|[.?!]\s+)表示捕获.(点),?!,后跟一个或多个空格字符(制表符,空格等)。 ^表示字符串的开头;因此,该组完全表示句子的开头或.?!,后跟空白。
  • (\w+)?表示一个或多个单词字符
  • 大写功能然后保留在第一组上匹配的内容,并在第二组(单词)上大写。