Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > regex showdown

Reply
Thread Tools

regex showdown

 
 
si
Guest
Posts: n/a
 
      04-20-2005

hi, i have a regex question for the gurus here

str = "aa (b c d) ee ff=1.7e-006 gg=4.1 hh=1 ii=on"

notes:
- the string between the parenthesis can be any length,
eg b c d b1 c1 d1 b2 c2 etc.... )
- there can be any number of name=value pairs at the end of the
expression
eg: ff=1.7e-006 gg=4.1 hh=1 ii=on jj=2.5 kk=connected etc


what would be regex that would return the below?

$name = 'aa'

@friends = ('b', 'c', 'd')

$alias = 'ee'

%pairs = ( 'ff', 1.7e-006,
'gg', 4.1,
'hh', 1, etc .. )


thx thx thx!

 
Reply With Quote
 
 
 
 
phaylon
Guest
Posts: n/a
 
      04-20-2005
si wrote:

> what would be regex that would return the below?


What have you tried? I'm always interested in getting better, but that
doesn't mean that I'm gonna do other people's jobs.

--
http://www.dunkelheit.at/

»Better to reign in hell than to serve in heaven«
-- John Milton, »Paradise Lost«

 
Reply With Quote
 
 
 
 
Mark Clements
Guest
Posts: n/a
 
      04-20-2005
si wrote:
> hi, i have a regex question for the gurus here
>
> str = "aa (b c d) ee ff=1.7e-006 gg=4.1 hh=1 ii=on"
>
> notes:
> - the string between the parenthesis can be any length,
> eg b c d b1 c1 d1 b2 c2 etc.... )
> - there can be any number of name=value pairs at the end of the
> expression
> eg: ff=1.7e-006 gg=4.1 hh=1 ii=on jj=2.5 kk=connected etc
>
>
> what would be regex that would return the below?
>
> $name = 'aa'
>
> @friends = ('b', 'c', 'd')
>
> $alias = 'ee'
>
> %pairs = ( 'ff', 1.7e-006,
> 'gg', 4.1,
> 'hh', 1, etc .. )
>


Break the problem into chunks: you might be able to do most of it in one
go, but it would be nearly impossible to maintain.

Hint: your test string has four distinct parts. Take it from there.

Other here posters could easily provide you with a regex to do this
first step (which is the meat of the problem), but as has been pointed
out, you don't appear to have tried anything yet and this place is
supposed to be for asking advice, not for asking others to do your work.


Mark
 
Reply With Quote
 
si
Guest
Posts: n/a
 
      04-20-2005
sorry guys, i have tried a few things but they didn't work out so i
didn't post them... here's my latest attempt

i'm kind of a newbie with regexp so don't laugh


/^(?<name>\w+)(*(?<pins>\w+)\s*)*(?<masterNm>\w)\s* (?<lhs>\w+)\s*=\s*(?<rhs>\w+)/

$name = $1;
@friends = $2;
$alias = $3
%pairs ....

i was trying to avoid a while loop to populate @friends and %pairs but
i couldn't figure this out from my Friedl book

just trying to get a handle with this stuff...

thx

 
Reply With Quote
 
si
Guest
Posts: n/a
 
      04-20-2005

sorry that should have read

/^(?\w+)(*(?\w+)\s**)*(?\w)\s*(?\w*+)\s*=\s*(?\w+)/

thx

 
Reply With Quote
 
Mark Clements
Guest
Posts: n/a
 
      04-20-2005
si wrote:
> sorry guys, i have tried a few things but they didn't work out so i
> didn't post them... here's my latest attempt
>
> i'm kind of a newbie with regexp so don't laugh
>
>
> /^(?<name>\w+)(*(?<pins>\w+)\s*)*(?<masterNm>\w)\s* (?<lhs>\w+)\s*=\s*(?<rhs>\w+)/
>
> $name = $1;
> @friends = $2;
> $alias = $3
> %pairs ....
>
> i was trying to avoid a while loop to populate @friends and %pairs but
> i couldn't figure this out from my Friedl book
>
> just trying to get a handle with this stuff...
>
> thx
>


> sorry that should have read


> /^(?\w+)(*(?\w+)\s**)*(?\w)\s*(?\w*+)\s*=\s*(?\w+)/


OK - you are misunderstanding what ? and * do, and when () need to be
escaped (and when they don't). You need to read man perlre carefully.
Also have a look at man re - there are debug options that can be turned
on for regular expressions that may help you.

/(\w+)\s+\(((?:\w\s?)*)\)\s+(\w+)\s(.*)/);

*seems* to work for me. Make sure you understand what the regex is doing.

You can then use split and map to populate @friends and %pairs. Look at

perldoc -f split
perldoc -f map


regards,

Mark
 
Reply With Quote
 
Steven Kuo
Guest
Posts: n/a
 
      04-20-2005
On 20 Apr 2005, si wrote:

> sorry guys, i have tried a few things but they didn't work out so i
> didn't post them... here's my latest attempt
>
> i'm kind of a newbie with regexp so don't laugh
>
>
> /^(?<name>\w+)(*(?<pins>\w+)\s*)*(?<masterNm>\w)\s* (?<lhs>\w+)\s*=\s*(?<rhs>\w+)/
>
> $name = $1;
> @friends = $2;
> $alias = $3
> %pairs ....
>
> i was trying to avoid a while loop to populate @friends and %pairs but
> i couldn't figure this out from my Friedl book
>
> just trying to get a handle with this stuff...





Assuming you can reliably use space, the equal sign, and parentheses
as delimiters, you can try:

use Data:umper;

$_ = 'aa (b c d) ee ff=1.7e-006 gg=4.1 hh=1 ii=on';
my $count_hash_elements = 0;
@_ = $_ =~ /([^ ()=]+)(?==(?{++$count_hash_elements}))?/g;
my ($alias, %hash) = splice(@_, -1 - 2 * $count_hash_elements);
my ($name, @friends) = @_;

print <<"__QED__";
NAME => $name
FRIENDS => @{[ Dumper \@friends]}
ALIAS => $alias
HASH => @{[ Dumper \%hash ]}
__QED__


What's wrong with using a while loop? If well written, it's much
easier to read and understand than what I've written. Take,
for example, the code here:

http://www.stonehenge.com/merlyn/UnixReview/col55.html

courtesy of Randal.

--
Hope this helps,
Steven

 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      04-20-2005
si <(E-Mail Removed)> wrote:

> str = "aa (b c d) ee ff=1.7e-006 gg=4.1 hh=1 ii=on"



> what would be regex that would return the below?
>
> $name = 'aa'
>
> @friends = ('b', 'c', 'd')
>
> $alias = 'ee'
>
> %pairs = ( 'ff', 1.7e-006,
> 'gg', 4.1,
> 'hh', 1, etc .. )



if ($str =~ s/(\S+)\s+\(([^)]+)\)\s+(\S+)\s+// ) {
$name = $1;
$alias = $3;
@friends = split /\s+/, $2;
%pairs = split /[ =]+/, $str;
}


--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Fabian Pilkowski
Guest
Posts: n/a
 
      04-21-2005
* Steven Kuo schrieb:
> On 20 Apr 2005, si wrote:
>>
>> i was trying to avoid a while loop to populate @friends and %pairs but
>> i couldn't figure this out from my Friedl book

>
> Assuming you can reliably use space, the equal sign, and parentheses
> as delimiters, you can try:
>
> use Data:umper;
>
> $_ = 'aa (b c d) ee ff=1.7e-006 gg=4.1 hh=1 ii=on';
> my $count_hash_elements = 0;
> @_ = $_ =~ /([^ ()=]+)(?==(?{++$count_hash_elements}))?/g;
> my ($alias, %hash) = splice(@_, -1 - 2 * $count_hash_elements);
> my ($name, @friends) = @_;
>
> print <<"__QED__";
> NAME => $name
> FRIENDS => @{[ Dumper \@friends]}
> ALIAS => $alias
> HASH => @{[ Dumper \%hash ]}
> __QED__
>
>
> What's wrong with using a while loop? If well written, it's much
> easier to read and understand than what I've written.


The idea behind your code looks good. But, why counting the equal signs
inside your regex and why splitting to @_. I'd write instead:


$_ = 'aa (b c d) ee ff=1.7e-006 gg=4.1 hh=1 ii=on';

my( $name, @friends ) = split /[ ()=]+/;
my( $alias, %pairs ) = splice @friends, -1 - 2 * tr/=//;


That's all. Splitting is done by split(), counting by tr/// and splice()
is asked to isolate the hash %pairs. I don't think, that avoiding a
while loop must be harder to read (btw, which loop at all? I haven't
seen one in this thread yet). Sure, one has to know what the used
functions will do, but therefor the docs are written.

regards,
fabian
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: wrap up show showdown PorchMonkey4Life DVD Video 11 12-02-2006 04:32 AM
No magazine has had a real showdown Rich Digital Photography 20 06-03-2006 05:30 AM
Asetek VapoChill Micro Showdown Silverstrand Front Page News 0 09-23-2005 11:35 AM
"3MP" Showdown George Preddy Digital Photography 18 07-07-2004 04:18 PM
Athlon XP vs. Pentium 4 : The Ultimate 32-bit CPU Showdown Big Cat@parxxx.net.nz NZ Computing 15 09-18-2003 11:29 PM



Advertisments