簡體   English   中英

如何進行正則表達式匹配和不匹配子字符串

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

您可以使用負前瞻進行此操作。

^(?!.*context).*\b(?:user|member)\.php.*$

現場演示

^(?=.*?(?:\buser\b|\bmember\b)\.php)(?:https?:\/\/)?[^/]*/(?:(?!context).)*$

您可以嘗試一下。請參閱演示。

http://regex101.com/r/yG7zB9/14

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM