0

I'm trying to get my head around using wordpress preg_ functions to filter strings using regex. Mainly just testing at this stage to try and understand how it all works.

So, I have this arbitrary string:-

random text [cbshort text="my text setting"] more random text
[cbshort2 text="my text setting"]some enclosed text[/cbshort2] yet
more text [cbshort text="my text setting"] some text on the end

which contains some shortcodes enclosed in square brackets. I'm trying to use regex to extract the shortcode sections from the string.

I tried using the following expression:

/\[cbshort.*\]/

Which seems to capture everything between the first [cbshort and the last ] in the string. I don't understand why its ignoring all the ] between the first and last. However, when I try:-

/\[cbshort.*?\]/

It seems to work correctly; it captures the individual sections between the square brackets.

I know the ? represents optional characters. But in the context of my example, I don't understand why my first example doesn't work and the second does.

Can anybody shed some light on it.

Sandesh
  • 1,190
  • 3
  • 23
  • 41

0 Answers0