How to use a variable as regular expression?

S

SamL

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

Jürgen Exner

SamL said:
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
 
S

SamL

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

Thanks a lot. It works.
 
S

SamL

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.


Excellent guess. What happened when you checked

        perldoc -f eval

jue

Thanks for your clarification. Yes eval is what I need.
 
T

Ted Zlatanov

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
 
T

Tad J McClellan

Ted Zlatanov said:
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.

:)
 
T

Ted Zlatanov

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top