Ignoring escaped literals

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Ignoring escaped literals

Post 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
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Ignoring escaped literals

Post by prometheuzz »

Try this:

Code: Select all

'/\{(\\\}|[^}])*\}/s'
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Ignoring escaped literals

Post by alex.barylski »

Negative.

This seems such a simpe problem...it drives me nutts I'm having such difficulty with it. :banghead: :lol:
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Ignoring escaped literals

Post 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.
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Ignoring escaped literals

Post 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.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Ignoring escaped literals

Post by josh »

Post Reply