Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Feb 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    perl code question

    Hi,

    I have below code :

    Code:
    my($abc) = "fred<hello>3hello"; 
    $abc =~ /^[^\d]{2,4}<([^>]+)>\d?\1$/; 
    
    if (defined($1)) { 
    print "$1\n"; 
    } else { 
    print "not found\n"; 
    
    } 
    }
    What is the code doing ?? what function of the regular expression
    Code:
     
    $abc =~ /^[^\d]{2,4}<([^>]+)>\d?\1$/;
    Please advice what it do ??

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by abcdef View Post
    Hi,

    I have below code :

    Code:
    my($abc) = "fred<hello>3hello"; 
    $abc =~ /^[^\d]{2,4}<([^>]+)>\d?\1$/; 
    
    if (defined($1)) { 
    print "$1\n"; 
    } else { 
    print "not found\n"; 
    
    } 
    }
    What is the code doing ?? what function of the regular expression
    Code:
     
    $abc =~ /^[^\d]{2,4}<([^>]+)>\d?\1$/;
    Please advice what it do ??
    - ^ is the begining of the string if is not between [ and ]
    - [something here] means a list of alowed chars or ranges of chars
    - the ^ inside means 'not match what follow'
    - \d is one of 0 to 9 ( as a note [^\d] is same as \D
    - {2,4} means 2 up to 4 times
    - < means to much <, obvious
    untill here $abc must start with number with minim 2 digit and maxim 4, something like 00 to 9999
    - (...) is a group, what is inside if match can be referenced later with \1 ... \9, based onf position, this is \1
    - [^>]+ mean anuthing exept > but at least one character
    - > means obious >
    - \d? nothing or a digit that mean could be 0 to 9 or miss
    - \1 is the reference to the group
    - $ end of $abc

    see perldoc for perlretut, perlrequick but i suggest to start with perlintro and learn perl

    best regards

  • #3
    New to the CF scene
    Join Date
    Feb 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by oesxyl View Post
    - ^ is the begining of the string if is not between [ and ]
    - [something here] means a list of alowed chars or ranges of chars
    - the ^ inside means 'not match what follow'
    - \d is one of 0 to 9 ( as a note [^\d] is same as \D
    - {2,4} means 2 up to 4 times
    - < means to much <, obvious
    untill here $abc must start with number with minim 2 digit and maxim 4, something like 00 to 9999
    - (...) is a group, what is inside if match can be referenced later with \1 ... \9, based onf position, this is \1
    - [^>]+ mean anuthing exept > but at least one character
    - > means obious >
    - \d? nothing or a digit that mean could be 0 to 9 or miss
    - \1 is the reference to the group
    - $ end of $abc

    see perldoc for perlretut, perlrequick but i suggest to start with perlintro and learn perl

    best regards
    So, could you advice in more detailed ??
    What will output for my($abc) = "fred<hello>3hello";
    What the regualr expression try to do/search for my($abc)???

  • #4
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by abcdef View Post
    So, could you advice in more detailed ??
    perldoc perlretut

    What will output for my($abc) = "fred<hello>3hello";
    not found

    What the regualr expression try to do/search for my($abc)???
    nothing

    best regards


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •