For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > July 2004 > Regex to do /<match>/<match>/<replace>?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Regex to do /<match>/<match>/<replace>?
Ian Marlier

2004-07-28, 8:56 pm

Hi, all --

I've got another RegEx question, a follow-up to one that I asked earlier
today:

Given a string that looks like this:
"This is a (string of words) that go together"

I need to turn it into this:
"This is a (stringofwords) that go together"

Which is to say, I need to match one set of characters (the parentheses) and
then do a reg-ex operation on another (the spaces).

Thoughts?

James Edward Gray II

2004-07-28, 8:56 pm

On Jul 26, 2004, at 8:17 PM, Ian Marlier wrote:

> Hi, all --
>
> I've got another RegEx question, a follow-up to one that I asked
> earlier
> today:
>
> Given a string that looks like this:
> "This is a (string of words) that go together"
>
> I need to turn it into this:
> "This is a (stringofwords) that go together"
>
> Which is to say, I need to match one set of characters (the
> parentheses) and
> then do a reg-ex operation on another (the spaces).
>
> Thoughts?


Do you have to worry about nested parens? It's a lot easier if you
don't:

s{(\([^)]\))}{
my $replace = $1;
$replace =~ tr/ //d;
$replace;
}e;

Hope that helps.

James

Gunnar Hjalmarsson

2004-07-28, 8:56 pm

Ian Marlier wrote:
> Given a string that looks like this:
> "This is a (string of words) that go together"
>
> I need to turn it into this:
> "This is a (stringofwords) that go together"
>
> Which is to say, I need to match one set of characters (the
> parentheses) and then do a reg-ex operation on another (the
> spaces).
>
> Thoughts?


You have some reading to do. Suggest that you start with:

perldoc perlrequick

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com