between square brackets inc. square brackets

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
Zugzwangle
Forum Newbie
Posts: 1
Joined: Mon Aug 30, 2010 3:50 am

between square brackets inc. square brackets

Post by Zugzwangle »

hi, what would the regex be, to find all content between square bracket including the brackets themselves? for example I want to find and delete [color=blue:2cd68fd71f] and [/color:2cd68fd71f]
MichaelR
Forum Contributor
Posts: 148
Joined: Sat Jan 03, 2009 3:27 pm

Re: between square brackets inc. square brackets

Post by MichaelR »

Code: Select all

'/\[[^\[\]]+\].+\[\/[^\[\]]+\]/'
This, perhaps?

Or if you want to distinguish between different things in between the brackets, you can be more specific like this, which works for the example you gave:

Code: Select all

'/\[color=[a-z]+:[a-f0-9]{10}\].+\[\/color=[a-z]+:[a-f0-9]{10}\]/i'
Last edited by MichaelR on Mon Aug 30, 2010 5:21 am, edited 1 time in total.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: between square brackets inc. square brackets

Post by josh »

(\[.*?\])

Micheal as far as I can tell he's not trying parse bbcode. He's trying to match the bbcode itself, not the text encapsulated within bb tags. Like a strip tags function made for removing unwanted bbcode formatting.
MichaelR
Forum Contributor
Posts: 148
Joined: Sat Jan 03, 2009 3:27 pm

Re: between square brackets inc. square brackets

Post by MichaelR »

Ah, I see. My mistake.
Post Reply