Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Help required on understanding regular expressions

by Marshall (Prior)
on Feb 20, 2012 at 07:46 UTC ( #954997=note: print w/ replies, xml ) Need Help??


in reply to Help required on understanding regular expressions

I don't see why you need to change anything.
Regex in Perl 5.10 is a superset of 5.8.
I modified your code slightly but it will run under both versions.
I think should also run under Perl 5.6.
I see no need here for fancy new features past what has been done for more than a decade.
There are new things that can be done.
But you may not have understood what could have been done in your prior code.

use strict; use Data::Dumper; my %hash=(); my $str="Test Tester Testing [Feb 18: 28_10_10] Test"; my $reg='(\w+) (\w+) (\w+) \[([\w\W]+)\] (.*?)'; my ($fname, $mname, $lname, $date) = $str =~ /$reg/is; if (defined ($date)) { $hash{fname}=$fname; $hash{mname}=$mname; $hash{lname}=$lname; $hash{date} =$date; } print Dumper \%hash; __END__ $VAR1 = { 'date' => 'Feb 18: 28_10_10', 'lname' => 'Testing', 'mname' => 'Tester', 'fname' => 'Test' };
Update:
If you must, although without spacing the regex out onto multiple lines, it is harder to understand...

use strict; use Data::Dumper; my %hash; my $str="Test Tester Testing [Feb 18: 28_10_10] TestXXX"; my $reg='(?<fname>\w+) (?<mname>\w+) (?<lname>\w+) \[(?<date>[\w\W]+)\ +]'; if ($str =~ /$reg/i) { %hash = %+; } print Dumper \%hash; __END__ $VAR1 = { 'lname' => 'Testing', 'date' => 'Feb 18: 28_10_10', 'fname' => 'Test', 'mname' => 'Tester' };


Comment on Re: Help required on understanding regular expressions
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://954997]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (9)
As of 2015-07-03 06:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (48 votes), past polls