regular expression match on the fly

A

a

Hi

#The str value is a variable, it is modified by the formula
my $str = a formula;
if ($my_str =~ /$str/) <-------Is it a valid RegEx comparison?
{
------
}

I have checked the documentation and I dont see any description on this?

Thanks
 
P

Peter Makholm

a said:
my $str = a formula;
if ($my_str =~ /$str/) <-------Is it a valid RegEx comparison?
{

Then the next best thing is to try it. It is (kind of) documented in
'perldoc perlop' under the heading 'Quote and Quote-like Operators'

//Makholm
 
M

Michele Dondi

#The str value is a variable, it is modified by the formula
my $str = a formula;
if ($my_str =~ /$str/) <-------Is it a valid RegEx comparison?

Well, it is syntactically valid. Whether it will do what you want is a
whole another matter. Is $str a regex or is it a plain string to be
matched literally? In the latter case check up \Q (and \E) in

perldoc perlre

and/or

perldoc -f quotemeta.


Michele
 
M

Mirco Wahab

a said:
#The str value is a variable, it is modified by the formula
my $str = a formula;
if ($my_str =~ /$str/) <-------Is it a valid RegEx comparison?

It *may* be a valid regular expression *match*.
I have checked the documentation and I dont see any description on this?

Don't create regular expressions on the fly
before you have *really* understood this
topic (Perl's Regular Expression engine).

Some characters in a string *will* get a
*special meaning*, if thrown into a regular
expression context.
Example:

....
{ #example 1
my $str = 'gshehshabh';
my $my_str = 'yyygshehshabhhshaheegdezsueiee';

if( $my_str =~ /$str/ ) {
print "match 1\n"
}
}

{ # example 2
my $str = '>=/|[^]|\=<';
my $my_str = 'yyy>=/|[^]|\=<-zzz';

if( $my_str =~ /$str/ ) {
print "match 2\n"
}
}
....

The strings in #1 would match, but
in #2 your program would bail w/error
message. Can you figure why?

Regards
 
A

a

Mirco Wahab said:
a said:
#The str value is a variable, it is modified by the formula
my $str = a formula;
if ($my_str =~ /$str/) <-------Is it a valid RegEx comparison?

It *may* be a valid regular expression *match*.
I have checked the documentation and I dont see any description on this?

Don't create regular expressions on the fly
before you have *really* understood this
topic (Perl's Regular Expression engine).

Some characters in a string *will* get a
*special meaning*, if thrown into a regular
expression context.
Example:

...
{ #example 1
my $str = 'gshehshabh';
my $my_str = 'yyygshehshabhhshaheegdezsueiee';

if( $my_str =~ /$str/ ) {
print "match 1\n"
}
}

{ # example 2
my $str = '>=/|[^]|\=<';
my $my_str = 'yyy>=/|[^]|\=<-zzz';

if( $my_str =~ /$str/ ) {
print "match 2\n"
}
}
...

The strings in #1 would match, but
in #2 your program would bail w/error
message. Can you figure why?

Regards

Hi,

In order to keep the regex valid, the \ has to be modified to be \\
the | has to be modified to be \| for your second example.

I guess, this is also my problem too, because i am using url to compare like
http://www.xxx.com where the . and the / are the sources of problem.

Thanks for your inspiration
 
M

Michele Dondi

In order to keep the regex valid, the \ has to be modified to be \\
the | has to be modified to be \| for your second example.

Just read on quotemeta() and \Q as hinted in my other post.


Michele
 

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,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top