如果我有这样的输入
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.
regex101Rextester
关于python - 正则表达式:删除点之前长度为 1-3 的字母,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68610059/