Code Comments
Programming Forum and web based access to our favorite programming groups.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
Post Follow-up to this messageMike 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.
****************************************
***************
Post Follow-up to this messageMike 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
Post Follow-up to this messageOn 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
Post Follow-up to this message> 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.
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.