Page 1 of 1

Ignoring escaped literals

Posted: Fri Dec 26, 2008 3:37 pm
by alex.barylski
I have the following simple regex:

Code: Select all

 
"/{[^}]+)}/";
 
This works awesome, but...

I would llike to support literal '}' by having them escaped soa a string like:

Code: Select all

{This is some text and an escaped \} which is not included as part of the string}
I imagine this is possible using look aheads? How can I match two characters ahead of time and skip the '}' is it's prefix by a escape slash?

EDIT | It occured to me it might be possible by adding an alternative match to the mix inside the negated operation, something like this:

Code: Select all

 
"/{[^}|\\}]+)}/";
 
However this doesn't seem to work so I hope I am missing something simple...???

Cheers,
Alex

Re: Ignoring escaped literals

Posted: Fri Dec 26, 2008 3:53 pm
by prometheuzz
Try this:

Code: Select all

'/\{(\\\}|[^}])*\}/s'

Re: Ignoring escaped literals

Posted: Fri Dec 26, 2008 4:29 pm
by alex.barylski
Negative.

This seems such a simpe problem...it drives me nutts I'm having such difficulty with it. :banghead: :lol:

Re: Ignoring escaped literals

Posted: Sat Dec 27, 2008 1:21 am
by prometheuzz
PCSpectra wrote:Negative.
...
Well, the following code:

Code: Select all

$text = '{abd \} def} ignore { ghi }';
if(preg_match_all('/\{(\\\}|[^}])*\}/', $text, $matches)) {
  print_r($matches);
}
captures both {abd \} def} and { ghi } from $text. If that is not what you're after, you'll have to explain yourself a bit more because that seems to me the desired output.

Re: Ignoring escaped literals

Posted: Sat Dec 27, 2008 1:44 am
by prometheuzz
B.t.w., there are a couple of things wrong with your original regex:

Code: Select all

'/{[^}|\\}]+)}/'
First, everything between square brackets (called a character class or character set) will only match one character. And many of the meta characters loose their special meaning inside a character class. So, this part of your regex:

Code: Select all

[^}|\\}]
matches any (single!) character except '}', '|'. Note that the pipe (the logical OR) has no special meaning inside a character class.

Second, you did not escape the opening bracket '{' in your original regex.

Re: Ignoring escaped literals

Posted: Wed Dec 31, 2008 3:54 am
by josh