Jump to content

Match two things that are similar


The Little Guy

Recommended Posts

I have these two regexp, and the first one doesn't work the way I wan't, because it is also matching the same stuff as the second regexp and replacing that too, which it shouldn't

 

$content = preg_replace("/\[block\.+)\=(.+)\](.+)\[\/block\]/isUe", "'<div class=\"lnavhead\">$1<span style=\"float:right;\">Returns: <span style=\"color: #ffffff;\">$2</span></span></div><div style=\"border: solid 1px #dddddd;padding:10px;\">'.sections('$3').'</div>'", $content);
$content = preg_replace("/\[block\.+)\](.+)\[\/block\]/isUe", "'<div class=\"lnavhead\">$1</div><div style=\"border: solid 1px #dddddd;padding:10px;\">'.sections('$2').'</div>'", $content);

 

Here are the two formats of each block, and my first regexp matches both and try's to do stuff.

[block:Methods]
[item:close()]Closes a connection with a file[/item]
[item:duplicate()]Duplicates a file[/item]
[item:read()]Reads a file[/item]
[item:save()]Saves a file[/item]
[item:truncate()]Deletes everything in a file[/item]
[/block]

[block:Parameters=self]
[item:$content]The content you want to save to the file[/item]
[item:$filename]The name you want to save the file as[/item]
[item:$overwrite](optional [Default = true]) Set to true to overwrite an existing file, set to false to create a new file with a number appended to it[/item]
[/block]

 

If you need more info let me know

Link to comment
Share on other sites

Even if the first regexp is lazy it will still match the entire input (both blocks) eventually.  It'll just take longer to find the match.  If the block you don't want to match never has an "=" in it, then you can replace all your dots with [^=] and that ought to fix it.  Or you can restrict the "=" check to within the open block tag itself, and further limit the character set inside there.  Eg if those open block tags only have alphanumeric stuff in them, I would be telling the regexp to only accept alphanumeric.

Link to comment
Share on other sites

Did you mean like this:

$content = preg_replace("/\[block\.+)\=(.+)\](.+)\[\/block\]/isUe", "'<div class=\"lnavhead\">$1<span style=\"float:right;\">Returns: <span style=\"color: #ffffff;\">$2</span></span></div><div style=\"border: solid 1px #dddddd;padding:10px;\">'.sections('$3').'</div>'", $content);
$content = preg_replace("/\[block\[^=]+)\]([^=]+)\[\/block\]/isUe", "'<div class=\"lnavhead\">$1</div><div style=\"border: solid 1px #dddddd;padding:10px;\">'.sections('$2').'</div>'", $content);

 

Here is what that does:

http://24.179.144.72:3333/docs/System/Files.php

Link to comment
Share on other sites

I was thinking of like this:

 

$content = preg_replace("/\[block\[[:alpha:]]+)\=([[:alpha:]]+)\](.+)\[\/block\]/isUe", "'<div class=\"lnavhead\">$1<span style=\"float:right;\">Returns: <span style=\"color: #ffffff;\">$2</span></span></div><div style=\"border: solid 1px #dddddd;padding:10px;\">'.sections('$3').'</div>'", $content);

 

So by specifying exactly what you want to match on either side of the "=", the expression is less likely to match the wrong thing.  The second expression would be:

 

$content = preg_replace("/\[block\[[:alpha:]]+)\](.+)\[\/block\]/isUe", "'<div class=\"lnavhead\">$1</div><div style=\"border: solid 1px #dddddd;padding:10px;\">'.sections('$2').'</div>'", $content);

 

And it won't match anything with an "=" in the opening block tag, because you've told it only to accept alphabetical characters followed by a closing square bracket.

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.