python 正则匹配后去掉正则表达式本身

import re

def remove_non_chinese(input_str):
    return "".join(re.findall(r'[\u4e00-\u9fa5]', input_str))

sentence = "我爱北京天安门"
rule = "我爱北京.{1,3}"
foundall = re.findall(rule, sentence)
rule_replace_part = remove_non_chinese(rule)
for found in foundall:
    print(found.replace(rule_replace_part,""))

print结果:
天安门

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章