Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

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


Report this thread to moderator Post Follow-up to this message
Old Post
Mike Lesser
03-29-05 01:56 AM


RE: More Regex for Newbies
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.
 ****************************************
***************


Report this thread to moderator Post Follow-up to this message
Old Post
Wagner, David --- Senior Programmer Analyst --- WG
03-29-05 01:56 AM


RE: More Regex for Newbies
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



Report this thread to moderator Post Follow-up to this message
Old Post
Charles K. Clarkson
03-29-05 01:56 AM


Re: More Regex for Newbies
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

Report this thread to moderator Post Follow-up to this message
Old Post
Offer Kaye
03-29-05 08:57 AM


Re: More Regex for Newbies

> 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.


Report this thread to moderator Post Follow-up to this message
Old Post
Mike Lesser
03-29-05 08:56 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PERL Beginners archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 06:50 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.