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

k3bvogb1  于 2023-06-25  发布在  其他
关注(0)|答案(1)|浏览(88)

我无法找到一个正则表达式的解决方案,它只在字符串的特定范围内查找模式
我想找到$ $,但是只有当它在字符串的5-7位置,并且这两个字符之间的字符无关紧要
示例

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

iswrvxsc1#

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

相关问题