Beefy Boxes and Bandwidth Generously Provided by pair Networks vroom
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

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?

'^(\S+)\s+(\S+)\s+(\S+)\s+\[([^\]]*)\]\s+\1\z'

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
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;

    Thanks.

      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;
      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.

        Thanks

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2014-04-21 04:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (490 votes), past polls