Page 1 of 1

Remove everything in brackets (and the brackets, too)

Posted: Thu May 03, 2007 8:01 am
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 :(

Posted: Thu May 03, 2007 8:05 am
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).

Posted: Thu May 03, 2007 8:12 am
by Grim...
Genius.

I'll post the results shortly :)

Posted: Thu May 03, 2007 8:33 am
by Grim...
Whoo!

Art, baby :D

Posted: Thu May 03, 2007 8:45 am
by jayshields
Nice! Who is it? Bill and Ted? :? Oh, and sweet domain!

Posted: Thu May 03, 2007 8:47 am
by Grim...
Bill and Ted rendered in the ASCII of the Bill and Ted script.

Posted: Thu May 03, 2007 9:04 am
by Chris Corbyn
Very nice :) What was the regexp for?

EDIT | I almost forgot.... Be Excellent!!

Posted: Thu May 03, 2007 9:20 am
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!

Posted: Thu May 03, 2007 5:22 pm
by Kieran Huggins
whoa... STATION!

Posted: Tue May 08, 2007 3:09 pm
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);

Posted: Tue May 08, 2007 4:25 pm
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.