[英]How to REGEX match and doesn't match substring
如果匹配和不匹配子字符串如何合並正則表達式?
例如:
http://www.domain.com/user.php?query=blah
http://www.domain.com/member.php?query=blah
http://www.domain.com/user.php?query=blah&context=DkUdh8ShTkNNkwoIJkl
http://www.domain.com/member.php?query=blah&context=DkUdDh8ShTaDfkNNkwoIJkl
我想獲取包含"user.php"
和"member.php"
URL,但是如果包含"context"
我不想獲取URL
要匹配包含“ user.php”和“ member.php”的網址,我可以使用:
(user\.php|user\.php)
並且如果不包含“上下文”:
^((?!context).)*$
如何合並(user\\.php|user\\.php)
和^((?!context).)*$
?
我的結果應該是:
http://www.domain.com/user.php?query=blah
http://www.domain.com/member.php?query=blah
感謝幫助...
使用這種模式
^(?=.*(?:user|member))(?!.*context).*$
做了:
^(?=.*\/(?:user|member)\b\.php)(?!.*context).*$
^ # begining of string
(?=.*\/(?:user|member)\.php) # look ahead for "/user.php" or "/memeber.php"
(?!.*context) # negative look ahead for "context"
.*$ # anything to the end
^(?=.*?(?:\buser\b|\bmember\b)\.php)(?:https?:\/\/)?[^/]*/(?:(?!context).)*$
您可以嘗試一下。請參閱演示。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.