Forum Moderators: phranque

Message Too Old, No Replies

More fun with regex, negative lookahead matching unexpectedly

         

csdude55

5:24 am on Oct 25, 2021 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I'm starting with this string:

$pattern = '(?:ab+c+)';


I want to match all [a-z] that are followed by *, ?, or +, UNLESS it's followed by ) followed by | or \Z (the end of the string).

So I have this:

/
[a-z]
[
*
?
+
]+
(?!
\)
[
|
\Z
]
)
/xg


For some reason it's matching both b+ AND c+, but I only want it to match b+.

What am I doing wrong?

lucy24

3:32 pm on Oct 25, 2021 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



UNLESS it's followed by ) followed by | or \Z (the end of the string)
Is \Z construed as a character like any other? In the usual construction involving $ (or, mutatis mutandis, ^ or \b), the
(?!\)[|\Z])
part would instead have to be
(?!\)\||\)\Z)
using pipes instead of grouping. Yes, two consecutive pipes--one escaped, one not--and repeated \) because lookaheads/behinds can’t be nested.

It works in SubEthaEdit, recognizing only b+. (Somewhere along the line SEE added \A and \Z to its default syntax, though I have never used it.)

Writing a RegEx to locate a RegEx is decidedly meta ;)

csdude55

5:42 pm on Oct 25, 2021 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Ahh, who knew? LOL That worked, thanks @lucy24!

Writing a RegEx to locate a RegEx is decidedly meta ;)

I'm still working on my profanity filter... I've been working on it for a little over TWO WEEKS, and it's still not quite finished :-O

My dream is to allow simple expressions in the database, and then the script will be able to figure it all out. It's gotten VERY complex, though, and I find myself catching a bug and then having to go back and rewrite something I thought was finished a week ago.

I'm starting to think it's a fool's errand, but since I'm the one that assigned it I guess that makes me twice the fool? Unless I can get it to work, then maybe I can sleep at night again :-D