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
https://forums.phpfreaks.com/topic/236270-match-two-things-that-are-similar/
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.

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

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.