Parsing for square brackets with regular expressions

I always had a hard time with regular expressions. I was looking for help with this, but I cannot find what I am looking for.

I have blocks of text that follow this pattern:

[Php] ... any type of code here [/ php]

I need:

  • check the square brackets, which can contain any number of 20-30 programming languages ​​( php , ruby , etc.).
  • you need to grab all the code between the opening and closing brackets.

I developed the following regular expression:

#\[([az]+)\]([^\[/]*)\[/([az]+)\]#i

Which fits all well. However, it is interrupted when the sample code contains square brackets. How to change it so that any character between these opening / closing brackets is matched for future use?

+4
source share
5 answers

This is the regular expression you want. It matches tags, even tags, so the php tag ends with the php tag.

 /\[(\w+)\](.*?)\[\/\1\]/s 

Or, if you want to explicitly match tags that you could use ...

 $langs = array('php', 'python', ...); $langs = implode('|', array_map('preg_quote', $langs)); preg_match_all('/\[(' . $langs . ')\](.*?)\[\/\1\]/s', $str, $matches); 
+5
source

The following will work:

 \[([az]+)\].*\[/\1\] 

If you do not want to remove greed, you can do:

 \[([az]+)\].*?\[/\1\] 

All you have to do is check that both the closing and opening tags have the same text (in this case, both are the same programming language), and you do this with \1 , telling it to matched the previously matched group number 1: ([az]+)

+1
source

Not sure if the language you use but follow inanimate regex should work for you:

 #\[([az]+)\](.*?)\[/(\1)\]#i 

Instead of looking for a non-opening-square-bracket you need to combine everything until you get [ using an inanimate modifier .*?

0
source

Why don't you use something like below:

 \[php\].*?\[/php\] 

I don’t understand why you want to use [az] + for tags, there must be php or a limited number of other tags. Just keep it simple.

You can actually use:

 \[(php)\].*?\[/(\1)\] 

so that you can match the opening and closing tags. Otherwise, you will match random opening and closing. Add others like, I don't know, js, etc. Like php|js etc.

0
source

Use the backlink to link to a match already made in the regular expression:

 \[(\w+)\].*?\[/\1\] 
0
source

All Articles