I’m trying to write a (I think) pretty simple RegEx with PHP but it’s not working.
Basically I have a block defined like this:
%%%%blockname%%%%
stuff goes here
%%%%/blockname%%%%
I’m not any good at RegEx, but this is what I tried:
preg_match_all('/^%%%%(.*?)%%%%(.*?)%%%%\/(.*?)%%%%$/i',$input,$matches);
It returns an array with 4 empty entries.
I guess it also, apart from actually working, needs some sort of pointer for the third match because it should be equal to the first one?
Please enlighten me 🙂
You need to allow the dot to match newlines, and to allow
^and$to match at the start and end of lines (not just the entire string):The
s(single-line) option makes the dot match any character including newlines.The
m(multi-line) option allows^and$to match at the start and end of lines.The
ioption is unnecessary in your regex since there are no case-sensitive characters in it.Then, to answer the second part of your question: If
blocknameis the same in both cases, then you can make that explicit by using a backreference to the first capturing group: