1

So I'm admittedly miserable with regular expressions, so if someone could help me out a bit here both in respect to figuring out how I can perform a particular search; but also how the applied regular expression actually works, I'd be ecstatic.

I'm trying to achieve placement/replacement of a 'user modifiable tag' in a PHP framework I'm building up.

The example is as follows:

$ReplacementOpenTag = "[{";
$ReplacementCloseTag = "}]";

$SomeRegex = "$ReplacementOpenTag(...)$ReplacementCloseTag";
$Matches = array();

if( preg_match_all($ReplacementSubject,$SomeRegex,&$Matches) <= 0 )
    return;
else
    foreach( $Matches as $Match ) // <--- Match?
        echo($Match[0]); // <--- Right?

Where the user would be able to set the Open and Close tag to say - "<{" or "[|" if so inclined - in case for some reason my 'standard' tags aren't suitable to their needs.

Now... I feel like this seems way too easy - but after some time fumbling around at http://tryphpregex.com/ and coming up short, repeatedly, I figure it's time to see if I can get a quick and easy solution to this presumably easy regex so that I can dissect it and figure out what I'm missing.

I feel like there should be a 'Stupid-Question' tag...

DigitalJedi805
  • 1,486
  • 4
  • 16
  • 41
  • possible duplicate of [Regular expression pattern not matching anywhere in string](http://stackoverflow.com/questions/4231382/regular-expression-pattern-not-matching-anywhere-in-string) – Jay Blanchard Nov 12 '14 at 17:43

1 Answers1

1

First, escape your delimiters, because they may contain special characters (and in your example, they do):

$ReplacementOpenTag = preg_quote($ReplacementOpenTag);
$ReplacementCloseTag = preg_quote($ReplacementCloseTag);

Next, build a regex, using a nongreedy pattern in between the delimiters:

$SomeRegex = "/$ReplacementOpenTag(.*?)$ReplacementCloseTag/s";

Then, just use it like this

preg_match_all($SomeRegex, $ReplacementSubject, $Matches);

Or, for the replacement:

preg_replace($SomeRegex, "<b>$1</b>", $ReplacementSubject);
Lucas Trzesniewski
  • 50,214
  • 11
  • 107
  • 158
  • Much appreciated - I just have one question for you at this point, as I'm trying to test this - how do I go about 'manually' escaping my delimiters? I've dumped this: '<{(*.?)}>' into that regex tester and as expected it's coming up with a bad pattern - I've tried slashes in both directions to no avail :-/. I just want to be thorough for the sake of my understanding. – DigitalJedi805 Nov 12 '14 at 17:35
  • 1
    Look at which characters are escaped by [`preg_quote`](http://php.net/manual/en/function.preg-quote.php) and escape them with a backslash. For your example: `<\{(.*?)\}>`. (`preg_quote` escapes `<` and `>` too but in your case this is not needed. `\<\{(.*?)\}\>` would be valid too though). – Lucas Trzesniewski Nov 12 '14 at 17:41
  • Right on - thank you. I believe I've just thoroughly lost faith in that regex tester - as an exact copy/paste still failed. I'll implement it and report back if I have any problems. *cheers* – DigitalJedi805 Nov 12 '14 at 17:44
  • Maybe [regex101](http://regex101.com) will be more helpful ;) BTW, when you copy/paste, beware of the escapes, and PHP's single/double quote handling. – Lucas Trzesniewski Nov 12 '14 at 17:54
  • Regex101 all day. Thank you sir. – DigitalJedi805 Nov 12 '14 at 18:46