#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Location
    Roseburg,OR
    Posts
    215
    Rep Power
    21

    Pattern Hunting to a function


    So in my string I have a bunch of html.

    I have these tokens in the document
    #randomNum(1)(100)#

    I am trying to parse those and pass the 2 values to a sub routine.

    This is my attempt, but its not working
    Code:
    $htmltemplate =~ s/(#randomNum\(([0-9]?):\)\(([0-9]?):\)#)/&randomnumber($1,$2)/g;

    The first number in #randomNum(1)(100)# is the min number, and the second is a max number, the sub routine merely passes back a random number in that section.


    Any regexp magicians able to show me up?

    Thanks in advance guys.!
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2007
    Posts
    765
    Rep Power
    929
    Expand out that regex and the differences from what you intend to parse should be clearer...

    Code:
    (                   # a group $1 containing...
        #               #   a hash
        randomNum       #   the text 'randomNum'
        \(              #   a left paren
        (               #   a group $2 containing...
            [0-9]       #       a digit
            ?           #       ...0 or 1 times
        )               #
        :               #   a colon
        \)              #   a right paren
        \(              #   a left paren
        (               #   a group $3 containing...
            [0-9]       #       a digit
            ?           #       ...0 or 1 times
        )               #
        :               #   a colon
        \)              #   a right paren
        #               #   a hash
    )                   #
    sub{*{$::{$_}}{CODE}==$_[0]&& print for(%:: )}->(\&Meh);
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    832
    Rep Power
    496
    Remove the two colons and replace thee two question marks with the plus (+) sign.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Location
    Roseburg,OR
    Posts
    215
    Rep Power
    21
    thank you OmegaZero and Laurent_R
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Location
    Roseburg,OR
    Posts
    215
    Rep Power
    21
    Code:
    $htmltemplate =~ s/#randomNum\(([0-9]+)\)\(([0-9]+)\)#/&randomnumber($1,$2)/g;
    The regexp works..

    But i can't pass to a subroutine like this.. Digging for a work around.

  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    832
    Rep Power
    496
    Try with the e modifier at the end:

    Code:
    $htmltemplate =~ s/#randomNum\(([0-9]+)\)\(([0-9]+)\)#/&randomnumber($1,$2)/ge;

    Comments on this post

    • RobSBG agrees : Ty
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Location
    Roseburg,OR
    Posts
    215
    Rep Power
    21
    Wow.! Gonna have to look that one up.

    Oooh Eval Modifier!

    Mental note made on that!

    Thanks ! That just totally blew my mind.!

IMN logo majestic logo threadwatch logo seochat tools logo