For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > March 2005 > More Regex for Newbies









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 More Regex for Newbies
Mike Lesser

2005-03-28, 8:56 pm

Hi all. I have this strange relationship with Regex. I seem to be able
to get simple
stuff accomplished, but in really brute-force ways, and I think I'm
missing some
fundamental aspect of its usage. I'm forced to chomp thru strings from
the
side like pac-man. It's like I can do a simple match or substitution,
but I can't
get stuff out of a string that I want (which is what seems to be going
on in all
the examples).

For example, if I have a string that goes like

"Joe Shmoe (alphanumerics)"

and I want to get the alphanumerics between the parens, It's like
pulling teeth,
I think (i think..) what I want to do is match the stuff that's _not_
between the
parens, and substitute that to nothing? Is that how to do it?

Mike

Wagner, David --- Senior Programmer Analyst --- WG

2005-03-28, 8:56 pm

Mike Lesser wrote:
> Hi all. I have this strange relationship with Regex. I seem to be able
> to get simple
> stuff accomplished, but in really brute-force ways, and I think I'm
> missing some
> fundamental aspect of its usage. I'm forced to chomp thru strings from
> the
> side like pac-man. It's like I can do a simple match or substitution,
> but I can't
> get stuff out of a string that I want (which is what seems to be going
> on in all
> the examples).
>=20
> For example, if I have a string that goes like
>=20
> "Joe Shmoe (alphanumerics)"

Are you after the 'alphanumerics' and replacing or selecting.

Something like:
my $MyAlpha =3D '';
if ( /^.+\(([^)])+\)/ ) {
$MyAlpha =3D $1;
}
So this would have alphanumerics in $MyAlpha.
^ Start of field
.+ 1 or more characters
\( find a left paren
([^)]+) Now gobble up to but not including the next )
$1 will hold what is between the Parens
\) find a right paren

A good place to start is a book by Friedl ( Mastering Regular Expressions =
) ISBN: 0596002890

Wags ;)=20
>=20
> and I want to get the alphanumerics between the parens, It's like
> pulling teeth,
> I think (i think..) what I want to do is match the stuff that's _not_
> between the
> parens, and substitute that to nothing? Is that how to do it?
>=20
> Mike




****************************************
***************
This message contains information that is confidential
and proprietary to FedEx Freight or its affiliates.
It is intended only for the recipient named and for
the express purpose(s) described therein.
Any other use is prohibited.
****************************************
***************

Charles K. Clarkson

2005-03-28, 8:56 pm

Mike Lesser <mailto:exceptions@earthlink.net> wrote:

: For example, if I have a string that goes like
:
: "Joe Shmoe (alphanumerics)"
:
: and I want to get the alphanumerics between the parens, It's like
: pulling teeth, I think (i think..) what I want to do is match the
: stuff that's _not_ between the: parens, and substitute that to
: nothing? Is that how to do it?

Start with the starting character and capture the interior by
matching everything except the closing character.

use strict;
use warnings;

my $phrase = 'Joe Shmoe (alphanumerics)';

print $1 if $phrase =~

m/
\( # Start with the starting character.

( # Start the capture.

[^)]+ # Capture everything in a class which
# excludes the closing character.

) # Close the capture.
/x;

__END__


HTH,

Charles K. Clarkson
--
Mobile Homes Specialist
254 968-8328


Offer Kaye

2005-03-29, 3:57 am

On Mon, 28 Mar 2005 18:07:20 -0500, Mike Lesser wrote:
> Hi all. I have this strange relationship with Regex. I seem to be able
> to get simple stuff accomplished, but in really brute-force ways,


The answer from Charles will help you up with your specific problem.
In addition, I heartly recommend that you read "perldoc perlrequick"
and "perldoc perlretut":
http://perldoc.perl.org/perlrequick.html
http://perldoc.perl.org/perlretut.html

--
Offer Kaye
Mike Lesser

2005-03-29, 3:56 pm


> I'm fairly new too, but
>
> s/\(.*\)//g
>
> Should work?
>

Thanks David. The "g" sort of throws me, but I had The Epiphany last
night. I'm still a little
bit , but I'm making headway now.

Sponsored Links







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

Copyright 2008 codecomments.com