I’ve always had a difficult time with regular expressions. I’ve searched for help with this, but I can’t quite find what I’m looking for.
I have blocks of text that follow this pattern:
[php]
… any type of code sample here
[/php]
I need to:
- check for the square brackets, which can contain any number of 20-30 programming language names (
php,ruby, etc.). - need to grab all code in between the opening and closing bracket.
I have worked out the following regular expression:
#\[([a-z]+)\]([^\[/]*)\[/([a-z]+)\]#i
Which matches everything pretty well. However, it breaks when the code sample contains square brackets. How do I modify it so that any character between those opening/closing braces will be matched for later use?
This is the regex you want. It matches where the tags are even too, so a
phptag will only end aphptag.Or if you wanted to explicitly match the tags you could use…