python - Word 文档中的正则表达式

标签 python regex ms-word

我有一个大文本文档,引号不一致,即

...Dolore magna aliquam “lorem ipsum” dolor sit amet, 'consectetuer adipiscing" elit, volutpat. Ut "wisi" enim...

我想将每种样式的引号转换为 Guillemet 样式(» 和 «),因此示例句子应该类似于

...Dolore magna aliquam »lorem ipsum« dolor sit amet, »consectetuer adipiscing« elit, volutpat. Ut »wisi« enim...

我正在考虑正则表达式

`` ["'”“](.*?)["'”“]``

但我只知道如何用Python编写代码。 有没有办法在 Python 中实现这一点?如果没有,有人可以提供有关如何直接在 MS Word 中完成此操作的提示。我尝试使用查找/替换和通配符,但使用引号的不一致让我感到困扰。

最佳答案

尝试这个模式:

([“'"](?=[a-zA-Z\,\.\s])([a-zA-Z\,\.\s]*)[”'"])

替换:

»$2«

编辑:既然你提到了Python,我就想出了一些绝对可行的东西:

#!/usr/bin/python
# coding: utf-8
import os, sys
import re
import codecs

with codecs.open('/path/to/file.txt', 'r', 'utf-8') as f:
    encoded = f.read()
    encoded = encoded.replace( u'\u201c', u'\"')
    encoded = encoded.replace( u'\u201d', u'\"')
    encoded = encoded.encode('utf-8')
    result = re.sub('(\s[\“\'\"](?=[a-zA-Z\,\.\s]*)([a-zA-Z\,\.\s]*)[\”\'\"]\s)', ' »\\2« ', encoded)
    decoded_result = result.decode('utf-8')
    print format(decoded_result)

/path/to/file.txt 替换为文件的位置(使用 utf-8 编码保存)。

由于标点符号中使用的字符编码,上面的代码执行了一些与标准搜索和替换不同的操作。可能有一种更简洁的方法来获得相同的最终结果,尽管整个编码过程对于 Python 来说很棘手,所以这是任何人的猜测。

关于python - Word 文档中的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22751134/

相关文章:

c++ - 如何在执行 LoadLibrary ("*.exe"时初始化 CRT)

python - 语法在客户转换器上提取日,月,年的语法错误

regex - 如何仅对包含大写字母的单词进行grep

ms-word - 使嵌入在网页中的 Word 文档可编辑或只读

html - 微软 Word/HTML : Export textbox as <div> and not as an image

python - 使用 Python 查找第 n 个素数

python - 字符串上的正则表达式包含转义字符

java - 是否有 Java 的 Matcher 类的 PHP 端口?

php - 正则表达式匹配所有字符,包括回车符

vba - 将单词内容存储在变量中