Jump to content

[SOLVED] Preg Syntax Question


Grego

Recommended Posts

Preg Syntax confuses me - I'm just starting to learn it.

 

Anyhow, I have this search term:

"/\{INCLUDE\.*)\}/is"

 

I want it to find {INCLUDE:sometext} on my page. However, my page has two of these statements on it:

{INCLUDE:sometext}
Bit in the middle
{INCLUDE:othertext}

So the preg_match is pulling out the following as a solution:

sometext}
Bit in the middle
{INCLUDE:othertext

 

How can I modify the syntax of "/\{INCLUDE\:(.*)\}/is" to make it only select the first following term, or only a solution before the end of a new line.

 

If I haven't explained that well, please ask questions to determine the problem better. Thanks in advance.

Link to comment
Share on other sites

Quantifiers are greedy by default. This means they take as much as possible and only backtrack to satisfy later matches.

 

\{INCLUDE\: matches literally.

(.*) gobbles up everything to the end of the string, then backtracks in order to match...

\}, thus matching the last } in the string.

 

Make the quantifier lazy, (.*?), or, even better, use ([^}]*).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.