Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Translating Perl Regular Expression syntax

Status
Not open for further replies.

jfreets2

Programmer
Joined
Apr 27, 2007
Messages
5
Location
US
I am new to reqular expressions with perl and I was wondering if someone could help explain the difference between using parenthesis around a regular expression and using (?:)

Also any translation on the following syntax would be appreciated.

(?{ $_{test} = $^N })

Thanks

Jason
 
When you use parenthesis, whatever was matched from inside of them gets put into $1, $2, $3, etc., but with the ?: in front, the matched value isn't put into those.

Code:
$var = "23:45:02";

if ($var =~ /(\d\d):(\d\d):(\d\d)/) {
   # $1 = 23
   # $2 = 45
   # $3 = 02
}

if ($var =~ /(\d\d):[COLOR=red](?:\d\d)[/color]:(\d\d)/) {
   # $1 = 23
   # $2 = 02
   # there is no $3
}

-------------
Cuvou.com | The NEW Kirsle.net
 
Thank you very much! That is exactly what I needed to know. What about the syntax below. Can you help me translate this also. How is (? used. Is this still part of a regular expression? It looks like $_{test} is perl syntax?

(?{ $_{test} = $^N })

Jason
 
A quick copy/paste from perlretut concerning $^N:

Code:
so that if the regexp matched, e.g., $2 would contain 'cd' or 'ef'. For convenience, perl sets $+ to the string held by the highest numbered $1, $2, ... that got assigned (and, somewhat related, $^N to the value of the $1, $2, ... most-recently assigned; i.e. the $1, $2, ... associated with the rightmost closing parenthesis used in the match).

$_{test} is, I'm assuming, using $_ as a hash and using the key "test" from it.

Alright, after scrolling much further through perlretut, I found some example snippits:

Code:
    $x = "abcdef";
    $x =~ /abc(?{print "Hi Mom!";})def/; # matches,
                                         # prints 'Hi Mom!'
    $x =~ /aaa(?{print "Hi Mom!";})def/; # doesn't match,
                                         # no 'Hi Mom!'

Pay careful attention to the next example:

    $x =~ /abc(?{print "Hi Mom!";})ddd/; # doesn't match,
                                         # no 'Hi Mom!'
                                         # but why not?

So, the (?{...}) syntax executes Perl code directly within the regular expression.

Now looking back at the one you posted, it sets $_{test} to be the last $1,$2,$3,...,$n variable matched in the regexp.

:-)

-------------
Cuvou.com | The NEW Kirsle.net
 
That is awesome! That helps a lot thank you. I have one more question if you have time. If I have a hash that contains several keys, when I call "join" using "keys" it creates a list of the key values divided by a string character correct? Such as key1 "|" Key2 "|" Key3 etc. I am having trouble translating the following:

join("|",
keys %MyHash,
map { my $c = $_;
$c =~ s/(\w)/$1./g;
( quotemeta $c, $_ ) }
sort { length $b <=> length $a }
values %MyHash)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top