remove brackets

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

Moderator: General Moderators

Post Reply
eyespark
Forum Commoner
Posts: 50
Joined: Tue Jan 24, 2006 7:36 am

remove brackets

Post by eyespark »

Hi,

I'm lost with this one. Example text:

Lorem ipsum dolor sit (text text text) amet, consectetur adipisicing elit, sed do eiusmod (text keyword1 text keyword2 text) tempor incididunt ut labore et dolore (text keyword3) magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation (text text text) ullamco laboris nisi ut aliquip ex ea commodo consequat.

I need to remove the brackets and its content only if the content contains keyword1 or keyword2 or keyword3.

I managed to remove all brackets, but this condition is killing for a few days now. Please help and thanks in advance.
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: remove brackets

Post by prometheuzz »

This ought to do it:

Code: Select all

$text = 'Lorem ipsum dolor sit (text text text) amet, consectetur adipisicing 
elit, sed do eiusmod (text keyword1 text keyword2 text) tempor incididunt ut 
labore et dolore (text keyword3) magna aliqua. Ut enim ad minim veniam, quis 
nostrud exercitation (text text text) ullamco laboris nisi ut aliquip ex ea 
commodo consequat.';
echo preg_replace('/\((?=[^)]*(?:keyword1|keyword2|keyword3))[^)]*\)/', '', $text);
eyespark
Forum Commoner
Posts: 50
Joined: Tue Jan 24, 2006 7:36 am

Re: remove brackets

Post by eyespark »

Oh, man, thank you!
You don't have to, but would you please explain this regex.
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: remove brackets

Post by prometheuzz »

eyespark wrote:Oh, man, thank you!
You're welcome.
eyespark wrote:You don't have to, but would you please explain this regex.
Not a problem. Here goes:

Code: Select all

\(                                 // match an opening parenthesis
(?=                                // start a positive look ahead
  [^)]*                            //   match zero or more characters other than a closing parenthesis
  (?:keyword1|keyword2|keyword3)   //   match 'keyword1', 'keyword2' or 'keyword3'
)                                  // stop the positive look ahead
[^)]*                              // match zero or more characters other than a closing parenthesis
\)                                 // match a closing parenthesis
In plain English this would look like:

Match an opening parenthesis only when there's one of the words 'keyword1', 'keyword2' or 'keyword3' ahead of it, and between the parenthesis and the keyword is not a closing parenthesis. If that first condition is met, then consume zero or more characters other than a closing parenthesis and lastly match a closing parenthesis.

Feel free to ask if there's something unclear to you.
eyespark
Forum Commoner
Posts: 50
Joined: Tue Jan 24, 2006 7:36 am

Re: remove brackets

Post by eyespark »

Great! Thanks again!
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: remove brackets

Post by prometheuzz »

eyespark wrote:Great! Thanks again!
No problem.
Post Reply