![](/img/trans.png)
[英]Printing the rest of a string after the second occurrence of a substring
[英]Match string after second occurrence of '/'
我正在尝试执行以下操作:
假设我们有一个迭代器返回如下所示的字符串:
/*/*/*/*/*/
其中 * 可以是任何字符串。 如果第二个 * 等于某个任意字符串,我想要一个匹配项,比方说“测试”。
/*/test/*/*/*/ <--- match
如果你真的需要一个正则表达式,那么你可以这样做:
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
这要求确实存在模式/*/test/*/*/*/
,其中*
是除/
之外的所有内容。
这应该做的工作
def check(s, index, searched_match):
return s.split('/')[index] == searched_match
print(check("/*/test/*/*/*/", 2, "test"))
>>> True
也可以使用split
方法的 maxsplit 参数(只有在确定test
后有东西的情况下才使用):
def check(s, index, searched_match):
return s.split('/', index + 1)[-2] == searched_match
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.