I'm trying to do the following:
Let's say we have an iterator returning strings looking like this:
/*/*/*/*/*/
where * can be any string. I would like a match if the second * is equal to some arbitrary string, lets say 'test'.
/*/test/*/*/*/ <--- match
If you really need a regex, then you can do it this way:
def check(s):
return re.match(r"\/[^\/]*\/test\/[^\/]*\/[^\/]*\/[^\/]*\/", s) is None
print(check("/one/test/three/four/five/"))
print(check("/one/two/three/four/five/"))
Output:
False
True
This requires that there is exactly the pattern /*/test/*/*/*/
, where *
is everything except for /
.
This should do the job
def check(s, index, searched_match):
return s.split('/')[index] == searched_match
print(check("/*/test/*/*/*/", 2, "test"))
>>> True
You can also use the maxsplit parameter of the split
method (use it only if you are sure that there is something after test
):
def check(s, index, searched_match):
return s.split('/', index + 1)[-2] == searched_match
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.