0

Building on this question: Regular expression to extract text between square brackets

this is a [sample] string with [some] special words. [another one]

I need to extract the text between [sample] and [some] plus the two boundaries too. In other word I want the regex that matches [sample] string with [some]

Community
  • 1
  • 1
SaidbakR
  • 13,303
  • 20
  • 101
  • 195

1 Answers1

3

Try this:

/\[sample\].*?\[some\]/

You just need to escape the square brackets and use a lazy match between your markers.

See it here on Regexr.

If the text can be on several rows, you need to enable the dotall mode additionally with the s modifier:

/\[sample\].*?\[some\]/s

this makes the . also match newline characters, what it is not doing by default.

See it here on Regexr

stema
  • 90,351
  • 20
  • 107
  • 135