简体   繁体   中英

The negative assertions in PHP regular expression

I try to find all the usernames in the form of http://www.test.com/username/ , but not in the form of http://www.test.com/username/ANY_VALID_URL_PART . I tried the negative assertions in PHP , but failed.

Let the content be:

<a href="http://www.test.com/username1/show" http://www.test.com/username2/ http://www.test.com/username3 target="_blank">

I want to get the username part in http://www.test.com/username/ and http://www.test.com/username But I also want to ignore the username in http://www.test.com/username/show . I tried the following:

preg_match_all("/(https?:\/\/www.test.com)\/([A-Za-z]+[A-Za-z0-9]+)(?<!(\/[A-Za-z0-9&?_-]))/i", $input_lines, $output_array);

But the result is:

Array
(
[0] => Array
    (
        [0] => http://www.test.com/username1
        [1] => http://www.test.com/username2
        [2] => http://www.test.com/username3
    )

[1] => Array
    (
        [0] => http://www.test.com
        [1] => http://www.test.com
        [2] => http://www.test.com
    )

[2] => Array
    (
        [0] => username1
        [1] => username2
        [2] => username3
    )

)

Could anyone tell me why the (?<!(\\/[A-Za-z0-9&?_-])) is not working, and how to fix it?

Even thought I can only choose one answer, I really appreciate all for your help!!!

Instead of Negative Lookbehind you should be using Negative Lookahead .

preg_match_all('~https?://www\.test\.com/([a-z0-9]+\b)(?!/[\w?&=-])~i', $str, $matches);
print_r($matches[1]);

Note: You need to escape the dots . in your regular expression as well. And if you only want to grab the username portion of the url you can use one capturing group, you don't need three here.

Live Demo

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