简体   繁体   中英

RegEx with character set inside positive lookbehind, Is it possible?

I need to match "name" only after "listing", but of course those words could be any url directory or page.

mydomain.com/listing/name

so the only thing I can "REGuest" (request) is to be some parent directory there. In other words, I want to match the "position" ie whatever comes 2nd after the domain.

I'm trying something like

(?<=mydomain\.com/[^/\?&]+/)[^/\?&]+(?:/)?

But the character set won't work inside the positive lookbehind, at least it's setup to match only ONE character. As soon as I try to match other than one (eg modify it with +, ? or *) it just stops working.

I'm obviously missing the positive lookbehind syntax and it seems not intended for what I'm trying.

How can I match that 2nd level filename?

Thanks.

Regular-expressions.info states that

The bad news is that most regex flavors do not allow you to use just any regex inside a lookbehind, because they cannot apply a regular expression backwards. Therefore, the regular expression engine needs to be able to figure out how many steps to step back before checking the lookbehind...

(Read further, they even mention Perl, Python and Java.)

I think the quantifier might be the problem. I found this on stackoverflow and briefly flew over it.

Wouldn't it be possible to just match the whole path, and use a group for the second level filename:

mydomain\.com\/[^\/\?&]+\/([^\/\?&]+)(?:\/)?

(note: I had to escape the / for my tests...)

The result of this would be something like:

Array
(
    [0] => mydomain.com/listing/name
    [1] => name
)

Now, because I don't know the context of your problem, I just assumed you would be able to postprocess the results and get the group 1 (index 1) from the result. If not, I unfortunately don't know...

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