python正则表达式,根据左右关键字,匹配中间内容

import re

stringA = "abcdefghijk"
pattern = re.compile(r'd(.+?)h')  # 匹配d和h中间的内容
print(re.findall(pattern, stringA))  # ['efg']

stringB = "abcdefg(python)hijk"
pattern = re.compile(r'[(](.*)[)]')  # 匹配括号内的内容
print(re.findall(pattern, stringB))  # ['python']

 

posted @ 2022-08-18 17:41  OTAKU_nicole  阅读(313)  评论(0编辑  收藏  举报