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
CodePudding user response:
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 /
.
CodePudding user response:
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