Python 正则匹配两个指定字符之间的字符方法

正则表达式太难了!!!

  1. 包含起始特定字符
result = re.findall(r'A.*C', s)
print(result)

>>['ABXC']
  1. 不包含起始特定字符
s = "ABXCXXD"
result = re.findall(r'A(.*)C', s)

result = re.findall(r'(?<=A)(.*)(?=C)', s)
print(result)

>>['BX']

.:匹配任意字符
*:匹配0个或多个字符
+:匹配1个或多个字符
?<=: 从指定字符向后查找
?=: 从指定字符向前查找

你可能感兴趣的:(Python,python)