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結果:
天安門

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