Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Re: Help required on understanding regular expressions

by ikegami (Pope)
on Feb 20, 2012 at 09:16 UTC ( #955020=note: print w/ replies, xml ) Need Help??

in reply to Help required on understanding regular expressions

A regex pattern that worked in 5.8 will also work in 5.10. What did you have in 5.8?


Well, that will continue to work in 5.10. If you wanted to use named captures,

'^(?<fname>\S+)\s+(?<mname>\S+)\s+(?<lname>\S+)\s+\[(?<date>[^\]]*)\]\ +s+\k<fname>\z'

PS — Make sure to test your code with the following string:

"Test Tester Testing [Feb 18: 28_10_10] Tests"

It shouldn't match ("Test" ne "Tests"), but it's easy to match it if the pattern isn't properly anchored.

Comment on Re: Help required on understanding regular expressions
Select or Download Code
Replies are listed 'Best First'.
Re^2: Help required on understanding regular expressions
by shekarkcb (Beadle) on Feb 20, 2012 at 10:42 UTC
    Thanks monks for the reply.

    I am not converting anything, just wanted to learn new feature, Still not able to get it working ... below is the code.

    use strict; use Data::Dumper; my %hash=(); my $str="Test Tester Testing [Feb 18: 28_10_10] Test"; #my $reg='(?<fname>\w+\b)\s(?<mname>\w+)\s(?<lname>\w+\s).*\[(?<date>[ +^\]]+)/'; my $reg='^(?<fname>\S+)\s+(?<mname>\S+)\s+(?<lname>\S+)\s+\[(?<date>[^ +\]]*)\]\+s+\k<fname>\z'; { if($str =~ /$reg/) { %hash=%+; } } print Dumper \%hash;

      Still not able to get it working
      As in, you get compile errors? Run time errors? Unexpected results? Or does the program sit on the couch, demanding beer and crips?

      Be specific, don't expect us to download your code and run it, just to find out what your "does not work" means. Tell us what you mean by "not working", and show us the errors and output you are getting.

        Thanks for all monks for the insights and flaws pointed out in my code and ways to improve.

        Well, i did clicked download link and downloaded code, it didn't work means - i didn't get any results in hash, and printing something on if loop also did not printed o/p on screen, that's what i was referring at. But looks like some peoples words are little more "harsh" than expected. I use to get my doubts on perl cleared in this website, but this time it was something else.


      You got typos :) this means start using /x so you can see them clearly

      my $reg = qr/ ^ (?<fname>\S+) \s+ (?<mname>\S+) \s+ (?<lname>\S+) \s+ \[ (?<date>[^\]]*) \] \+s+ # TYPOS \k<fname> \z /x;

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2015-10-04 17:08 GMT
Find Nodes?
    Voting Booth?

    Does Humor Belong in Programming?

    Results (103 votes), past polls