Python开发

Python正则分组获取和位置查找
贵鬼2024-10-18发布
82 0

1. 如果需要转义,开头加"r"

2. 分组获取

pat = re.compile(r'//\*\[@id="(.+)"\]')
matches = pat.findall('//*[@id="cc1"]')
for mat in matches:
  print(mat)

输出:cc1

3. 位置查询

xpath = '123#456#cc1'
matches = re.finditer('#', xpath)
positions = [match.start() for match in matches]
for i in positions:
  print(i)

输出:3,7