正则表达式但仅在子字符串中

时间:2019-03-13 14:37:22

标签: python regex

我找不到正则表达式的解决方案,该正则表达式仅在字符串的特定范围内寻找模式,但仅在字符串的特定范围内

我想找到$ $,但前提是它位于字符串的5-7位置,并且这两个之间的哪个字符都没有关系
例子

xxxx$x$xxxxx would match
xx$x$xxxxxxx would not

1 个答案:

答案 0 :(得分:1)

import re

should = "xxxx$x$xxxxx would match"
shouldnt = "xx$x$xxxxxxx would not"

pattern = r'^.{4}\$.\$.+'

re.match(pattern, should)
re.match(pattern, shouldnt)

给予

match
None

https://regex101.com/r/RLHrZb/1