python - 正则表达式:删除点之前长度为 1-3 的字母

标签 python regex

如果我有这样的输入

input  = 'AB. Hello word.'
the output should be 
output = 'Hello word.'
另一个例子是
input = 'AB′. Hello word'
output = Hello Word
我想生成一个代码,该代码适用于任何语言的任何字母组。这是我的代码
text = 'A. Hello word.'
text = re.sub(r'A\. \w{1,2}\.*', '', text)
text

output = llo word.
所以我可以用任何其他字母更改“A”,但由于某种原因效果不佳。
我也试过这个
text = 'Ab. Hello word.'
text = re.sub(r'A+\. \w{1,2}\.*', '', text)
text
output = Ab. Hello word.

但效果不佳。

最佳答案

尝试这个:

import re

regex = r"^[^.]{1,3}\.\s*"

test_str = ("AB. Hello word.\n"
    "AB′. Hello word.\n"
    "A. Hello word.\n"
    "Ab. Hello word.\n")

subst = ""

# You can manually specify the number of replacements by changing the 4th argument
result = re.sub(regex, subst, test_str, 0, re.MULTILINE)

if result:
    print (result)
输出:
Hello word.
Hello word.
Hello word.
Hello word.
regex101
Rextester

关于python - 正则表达式:删除点之前长度为 1-3 的字母,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68610059/

相关文章:

python - 使用 python 从重定向的标准输入读取输入

python - 使用嵌套字典从列表构建数据框

python - 带有lookbehind的正则表达式

python - 如何在 pyspark 的高基数分类列中有效地对低频计数级别进行分组?

python - 从 Vala 到 Python 再返回

python - python的 "openpyxl"包中如何绘制两组数据(折线图)

php - 匹配无限数量选项的正则表达式

regex - .htaccess 重写规则删除 RK=0/RS= 之后的所有内容

regex - Perl Regex - 在匹配时获取行号偏移量而不是字符位置

java - 对命名组使用 OR 运算符