简体   繁体   中英

How to REGEX match and doesn't match substring

How to combine regex if match and doesn't match substring?

Ex:

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

I want to get url which contain "user.php" and "member.php" BUT I don't want to get url if contain "context"

To match url if contain "user.php" and "member.php" I can use:

(user\.php|user\.php)

And if doesn't contain "context":

^((?!context).)*$

How to combine (user\\.php|user\\.php) and ^((?!context).)*$ ?

My results should be:

http://www.domain.com/user.php?query=blah
http://www.domain.com/member.php?query=blah

Thanks for help...

use this pattern
^(?=.*(?:user|member))(?!.*context).*$
make it:

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

Demo


^                           # begining of string
(?=.*\/(?:user|member)\.php)        # look ahead for "/user.php" or "/memeber.php"
(?!.*context)               # negative look ahead for "context"
.*$                      # anything to the end

You can use Negative Lookahead to do this.

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

Live Demo

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

You can try this.See demo.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM