SaidbakR
SaidbakR

Reputation: 13534

Regex to get the text between two text boundaries and the boundaries too

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]

Upvotes: 0

Views: 1184

Answers (1)

stema
stema

Reputation: 92976

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

Upvotes: 3

Related Questions