[英]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.