How to use a variable as regular expression?

Discussion in 'Perl Misc' started by SamL, Mar 23, 2009.

  1. SamL

    SamL Guest

    I want to use a variable as a regular expression because I want user
    to be able to specify the regular expression in command line.

    Basically I want something like this:

    perl -e '$pattern='s/a/b/'; $x='a'; $x=~$pattern; print $x;'

    where user can modify $pattern at runtime.

    Of course the above does not work. I am wondering if there is some
    mechanism in perl allowing me to do that. For example, something like
    eval in ksh.

    Thanks.
     
    SamL, Mar 23, 2009
    #1
    1. Advertising

  2. SamL <> wrote:
    >I want to use a variable as a regular expression because I want user
    >to be able to specify the regular expression in command line.
    >
    >Basically I want something like this:
    >
    >perl -e '$pattern='s/a/b/'; $x='a'; $x=~$pattern; print $x;'
    >
    >where user can modify $pattern at runtime.


    You seem to be thourougly confused.
    s/a/b/
    is neither a regular expression nor a pattern. In your context it is a
    string but ordinarily as code it would be a command, the substitute
    command to be precise.
    The only regular expression aka pattern is the 'a' in that line. And
    yes, there is no problem whatsoever using variables in regular
    expressions, just do it.

    That's what you asked for, but that is not your problem. Your problem is
    that you have data (the content of $pattern) and want that data
    evaluated as code. That is a completely different issue, applies to any
    data and any command, and has nothing whatsoever to do with regular
    expressions.

    >Of course the above does not work. I am wondering if there is some
    >mechanism in perl allowing me to do that. For example, something like
    >eval in ksh.


    Excellent guess. What happened when you checked

    perldoc -f eval

    jue
     
    Jürgen Exner, Mar 23, 2009
    #2
    1. Advertising

  3. SamL

    SamL Guest

    On Mar 23, 12:01 pm, (Greg Bacon) wrote:
    > SamL wrote
    >
    > : I want to use a variable as a regular expression because I want user
    > : to be able to specify the regular expression in command line.
    > :
    > : Basically I want something like this:
    > :
    > : perl -e '$pattern='s/a/b/'; $x='a'; $x=~$pattern; print $x;'
    > :
    > : where user can modify $pattern at runtime.
    > :
    > : Of course the above does not work. I am wondering if there is some
    > : mechanism in perl allowing me to do that. For example, something like
    > : eval in ksh.
    >
    > Yes, and it's called eval even!
    >
    > perl -le '$pattern="s/a/b/"; $x="a"; eval "\$x=~$pattern"; print $x'
    >
    > Hope this helps,
    > Greg
    > --
    > Criminal means, once tolerated, are soon preferred.
    >     -- Edmund Burke


    Thanks a lot. It works.
     
    SamL, Mar 23, 2009
    #3
  4. SamL

    SamL Guest

    On Mar 23, 12:31 pm, Jürgen Exner <> wrote:
    > SamL <> wrote:
    > >I want to use a variable as a regular expression because I want user
    > >to be able to specify the regular expression in command line.

    >
    > >Basically I want something like this:

    >
    > >perl -e '$pattern='s/a/b/'; $x='a'; $x=~$pattern; print $x;'

    >
    > >where user can modify $pattern at runtime.

    >
    > You seem to be thourougly confused.
    >         s/a/b/
    > is neither a regular expression nor a pattern. In your context it is a
    > string but ordinarily as code it would be a command, the substitute
    > command to be precise.
    > The only regular expression aka pattern is the 'a' in that line. And
    > yes, there is no problem whatsoever using variables in regular
    > expressions, just do it.
    >
    > That's what you asked for, but that is not your problem. Your problem is
    > that you have data (the content of $pattern) and want that data
    > evaluated as code. That is a completely different issue, applies to any
    > data and any command, and has nothing whatsoever to do with regular
    > expressions.
    >
    > >Of course the above does not work. I am wondering if there is some
    > >mechanism in perl allowing me to do that. For example, something like
    > >eval in ksh.

    >
    > Excellent guess. What happened when you checked
    >
    >         perldoc -f eval
    >
    > jue


    Thanks for your clarification. Yes eval is what I need.
     
    SamL, Mar 23, 2009
    #4
  5. SamL

    Ted Zlatanov Guest

    On Mon, 23 Mar 2009 08:35:42 -0700 (PDT) SamL <> wrote:

    S> I want to use a variable as a regular expression because I want user
    S> to be able to specify the regular expression in command line.

    S> Basically I want something like this:

    S> perl -e '$pattern='s/a/b/'; $x='a'; $x=~$pattern; print $x;'

    S> where user can modify $pattern at runtime.

    S> Of course the above does not work. I am wondering if there is some
    S> mechanism in perl allowing me to do that. For example, something like
    S> eval in ksh.

    Be careful, if the user can type any regular expression, they can
    execute plenty of Perl code. I think Perl 6 will have better support
    for what you want (I'm not sure, it's been a while since I read the
    relevant docs), but with Perl 5 you have to do the due dilligence
    yourself.

    Ted
     
    Ted Zlatanov, Mar 24, 2009
    #5
  6. Ted Zlatanov <> wrote:
    > On Mon, 23 Mar 2009 08:35:42 -0700 (PDT) SamL <> wrote:
    >
    > S> I want to use a variable as a regular expression because I want user
    > S> to be able to specify the regular expression in command line.

    ^^^^^^^^^^^^^^^
    ^^^^^^^^^^^^^^^
    > S> Basically I want something like this:
    >
    > S> perl -e '$pattern='s/a/b/'; $x='a'; $x=~$pattern; print $x;'
    >
    > S> where user can modify $pattern at runtime.
    >
    > S> Of course the above does not work. I am wondering if there is some
    > S> mechanism in perl allowing me to do that. For example, something like
    > S> eval in ksh.
    >
    > Be careful, if the user can type any regular expression, they can
    > execute plenty of Perl code.



    They already have command line access, so users can already
    execute plenty of Perl code.

    :)


    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
     
    Tad J McClellan, Mar 24, 2009
    #6
  7. SamL

    Ted Zlatanov Guest

    On Tue, 24 Mar 2009 15:25:20 -0500 Tad J McClellan <> wrote:

    TJM> Ted Zlatanov <> wrote:
    >> On Mon, 23 Mar 2009 08:35:42 -0700 (PDT) SamL <> wrote:
    >>

    S> I want to use a variable as a regular expression because I want user
    S> to be able to specify the regular expression in command line.
    TJM> ^^^^^^^^^^^^^^^
    TJM> ^^^^^^^^^^^^^^^
    >> Be careful, if the user can type any regular expression, they can
    >> execute plenty of Perl code.


    TJM> They already have command line access, so users can already
    TJM> execute plenty of Perl code.

    It's not the same thing. The command line can easily be

    sudo script.pl regex-here

    After reading `perldoc perlre' more carefully, though, it seems that
    this particular feature is disabled when the regex has an interpolated
    variable in it, and has to be explicitly turned on. So it's not so bad
    for the general case.

    Ted
     
    Ted Zlatanov, Mar 25, 2009
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark Fletcher
    Replies:
    1
    Views:
    492
    Mark Fletcher
    May 19, 2004
  2. VSK
    Replies:
    2
    Views:
    2,307
  3. Replies:
    5
    Views:
    464
    Steve Holden
    Aug 2, 2007
  4. Replies:
    1
    Views:
    105
    Jeff Pritchard
    Jul 19, 2006
  5. Tony
    Replies:
    2
    Views:
    128
    Tad McClellan
    Apr 21, 2005
Loading...

Share This Page