Remove everything in brackets (and the brackets, too)

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

Moderator: General Moderators

Post Reply
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Remove everything in brackets (and the brackets, too)

Post by Grim... »

Hi all.
I've got some text (which you might recognise)
Bill: Mr. Ryan, fellow distinguished classmates, teachers, babes. Ted: Our first speaker was born in the year… (Missy sneaks in and sits down next to Mr. Ryan.) Missy: Hi, long time no see. (Mr. Ryan smiles at her.) Ted: …470 BC. A time when much of the world looked like the cover of the Led Zepplin album 'Houses of the Holy'. Bill: We were there. There were many steps and columns, it was most tranquil. (gives a thumbs up.) Ted: He is sometimes known as the father of modern thought. He was the teacher of Plato, who was in turn the teacher of Aristotle. And like Ozzy Osborne, was repeatedly accused of corruption of the young. (Mr. Ryan watches them with interest.) Bill: And since he doesn't speak English, my friend Ted here, is going to interpret for him. (Ted shrugs his consent.) So please welcome, to tell us what he thinks of San Dimas, the most bodacious philosophizer in Ancient Greece… Both: Socrates! (The spotlight hits Socrates and he looks a little stumped.) (School Parking Lot) (Captain Logan and Mr. Preston are hurrying into the school.) Captain Logan: …is totally out of control. And I got a pretty good idea where he gets it from. I mean if you and that, that wife of yours would show a little discipline you're son wouldn't be such a bad influence. Mr. Preston: Oh, is discipline the key to the success with Ted? (goes inside) Captain Logan: (following) Yes! He's going to the Alaskan Military School. (They enter the Auditorium and see the boys on stage. Ted is speaking for Socrates.) Ted: He loves you best in all the world. Captain Logan: What are they doing up there? (Mr. Preston shhhs him.) Ted: He also loves baseball!
etc.

I'd like to remove all the stuff in the brackets, and the brackets themselves.
This is what I've got:

Code: Select all

$script = preg_replace("(.*)", "", $script, 1);
And it doesn't work.

I think the final '1' is there to stop it being greedy, but I'm not really sure.

I hate regex :(
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Code: Select all

$script = preg_replace("/\\(.*?\\)/s", "", $script);
//or even better
$script = preg_replace("/\\([^\\)]*\\)/", "", $script);
You were missing PCRE delimiters (/pattern/) and you needed to escape your parentheses. The "?" I added is the best way to make a quantifier ungreedy. Bear in mind that this will not work as expected for brackets inside brackets.

Today I was really hot (well, warm (sorta) but not hot).

-->

Today I was really hot but not hot).
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

Genius.

I'll post the results shortly :)
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

Whoo!

Art, baby :D
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post by jayshields »

Nice! Who is it? Bill and Ted? :? Oh, and sweet domain!
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

Bill and Ted rendered in the ASCII of the Bill and Ted script.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Very nice :) What was the regexp for?

EDIT | I almost forgot.... Be Excellent!!
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

The original script was too long with all the artistic directions in it.
With just the dialogue it was spot on :)

Party on, Dude! And happy Birthday!
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

whoa... STATION!
User avatar
GeertDD
Forum Contributor
Posts: 274
Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium

Post by GeertDD »

d11wtq wrote:

Code: Select all

$script = preg_replace("/\\(.*?\\)/s", "", $script);
//or even better
$script = preg_replace("/\\([^\\)]*\\)/", "", $script);
Using single quotes would make your regex a tad more readible. Just a small tip.

Code: Select all

$script = preg_replace('/\(.*?\)/s', '', $script);
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

GeertDD wrote:
d11wtq wrote:

Code: Select all

$script = preg_replace("/\\(.*?\\)/s", "", $script);
//or even better
$script = preg_replace("/\\([^\\)]*\\)/", "", $script);
Using single quotes would make your regex a tad more readible. Just a small tip.

Code: Select all

$script = preg_replace('/\(.*?\)/s', '', $script);
Thanks. I know I keep using double quotes these days... just habit because I keep bouncing between different programming languages.
Post Reply