On Thu, May 27, 2004 at 09:59:05AM -0700, Dan Phiffer wrote:
> So I'm trying to implement a simple wiki-like syntax for hyperlinking. 
> Basically I want to match stuff like [this], where the word 'this' gets 
> turned into a hyperlink. I have that working, but I want to be able to 
> escape the opening bracket, so that it's possible to do \[that] without 
> having it match as a link. Here's what I've got:
> 
> // Matches fine, but without escaping
> $pattern = "
>     /
>         \[          # Open bracket
>         ([^\]]+?)   # Text, including whitespace
>         \]          # Close bracket
>     /x
> ";
> 
> // Throws an unmatched bracket warning
> $pattern = "
>     /
>         [^\\]       # Don't match if a backslash precedes
>         \[          # Open bracket
>         ([^\]]+?)   # Text, including whitespace
>         \]          # Close bracket
>     /x
> ";
> 
> // Ignores escaping: \[example] still matches
> $pattern = "
>     /
>         [^\\\]      # Don't match if a backslash precedes
>         \[          # Open bracket
>         ([^\]]+?)   # Text, including whitespace
>         \]          # Close bracket
>     /x
> ";
> 
> Nothing seems to change if I keep adding backslashes to that first 
> matching thingy (i.e. the escaping still doesn't work). Any ideas?
> 

Try negative lookbehinds...

  $pattern = '
     /
        (?<!\\\\) \[    # open [ not preceded by a backslash
        (.*?)           # ungreedy match anything
        (?<!\\\\) \]    # close ] not preceded by a backslash
     /x
  ';

- Rob

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to