CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
/^((-*)|(\s*))$/ aahhh

 



fenniger
Novice

Nov 2, 2008, 6:47 AM

Post #1 of 4 (536 views)
/^((-*)|(\s*))$/ aahhh Can't Post

hi...

sorry for this one, but I'm sitting here for hours trying
to figure out what this regex means...
$something =~ /^((-*)|(\s*))$/
here is what I figured:
at the start there is a "-" and then whatever
or
at the end there is some string

is that what it means?


shawnhcorey
Enthusiast


Nov 2, 2008, 8:32 AM

Post #2 of 4 (534 views)
Re: [fenniger] /^((-*)|(\s*))$/ aahhh [In reply to] Can't Post


In Reply To
hi...

sorry for this one, but I'm sitting here for hours trying
to figure out what this regex means...
$something =~ /^((-*)|(\s*))$/
here is what I figured:
at the start there is a "-" and then whatever
or
at the end there is some string

is that what it means?


The /x modifier comes in handy:

Code
$something =~ / ^        # start at beginning of the string 
( # capture var $1
( # capture var $2
-* # zero, one, or more '-'
) # end capture var $2
| # alternative pattern if -* fails
( # capture var $2, again
\s* # zero, one, or more white space
) # end capture var $2
) # end capture var $1
$ /x; # end of string

Code
 
It captures in $1 and $2 blank lines, lines full of '-' and lines with not but white space.

__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


lmmilewski
Novice

Nov 3, 2008, 1:27 AM

Post #3 of 4 (525 views)
Re: [shawnhcorey] /^((-*)|(\s*))$/ aahhh [In reply to] Can't Post

Shouldn't it be


Code
$something =~ / ^        # start at beginning of the string  
( # capture var $1
( # capture var $2
-* # zero, one, or more '-'
) # end capture var $2
| # alternative pattern if -* fails
( # capture var $3, again
\s* # zero, one, or more white space
) # end capture var $3
) # end capture var $1
$ /x; # end of string


There is $3 instead of $2. I checked that on simple example.


Code
foreach (<STDIN>) { 
m/^((-*)|(\d*))$/;
print "[$1,$2,$3]\n";
}


For input:

Quote
------------
9999999999

I got

Quote
[------------,------------,]
[9999999999,,9999999999]


Am I missing something?
--
Łukasz Milewski


shawnhcorey
Enthusiast


Nov 3, 2008, 5:26 AM

Post #4 of 4 (519 views)
Re: [lmmilewski] /^((-*)|(\s*))$/ aahhh [In reply to] Can't Post


In Reply To
There is $3 instead of $2. I checked that on simple example.

Am I missing something?


Oops. You are right; I made a mistake.

The rule is: start counting opening parentheses from the left; the count is the number of the capture variable.

With some more thought, this match does not seem to be very useful. There are only three possible outcomes. If it is matched against $_, the values of $1, $2, and $3 are, respectively:

Case 1) undef, undef, undef
Case 2) $_, $_, undef
Case 3) $_, undef, $_

__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives