My regexp stupidity needs assistance before loose all my hair!

I had an escape character match in the regexp:

  / [^`] \[(.+?)\] /x

That was messing it up (Don't really know why) but I just "zeroed"

it:

  / (?=[^`]) \[(.+?)\] /x

And that did the trick.

Thats because [^`] will match 'a single character that is not `'.
When you did the zero-width lookahead, you made into 'possibly a
character, so long as it's not ` '.

Hope this makes sense :slight_smile:

I may be reading this wrong, but I think that with the zero-width
lookahead, it is now ensuring that the first character of the match is
not a backtick. Which, since it's always going to be a square bracket,
makes the lookahead superfluous.

If you need escaping, try:
/(?: # escape sequence match
    ^ | [^`] # alternate: match either "start of line" or a non-backtick.
  )
  ( # non-greedy [foo] match
    \[.*?\]
)/

... then use $1. This one won't match any paired square brackets
immediately preceded by a backtick.

cheers,
Mark

···

On Tue, 18 Jan 2005 07:31:06 +0900, Assaph Mehr <assaph@gmail.com> wrote:

> I had an escape character match in the regexp:
>
> / [^`] \[(.+?)\] /x
>
> That was messing it up (Don't really know why) but I just "zeroed"
it:
>
> / (?=[^`]) \[(.+?)\] /x
>
> And that did the trick.

Thats because [^`] will match 'a single character that is not `'.
When you did the zero-width lookahead, you made into 'possibly a
character, so long as it's not ` '.