Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > regular expression match on the fly

Reply
Thread Tools

regular expression match on the fly

 
 
a
Guest
Posts: n/a
 
      07-27-2007
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


 
Reply With Quote
 
 
 
 
Peter Makholm
Guest
Posts: n/a
 
      07-27-2007
"a" <(E-Mail Removed)> writes:

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


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
 
Reply With Quote
 
 
 
 
Michele Dondi
Guest
Posts: n/a
 
      07-27-2007
On Fri, 27 Jul 2007 06:27:36 GMT, "a" <(E-Mail Removed)> wrote:

>#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
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
 
Reply With Quote
 
Mirco Wahab
Guest
Posts: n/a
 
      07-27-2007
a wrote:
> #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
 
Reply With Quote
 
a
Guest
Posts: n/a
 
      07-27-2007

"Mirco Wahab" <(E-Mail Removed)>
???????:f8c91m$l8j$(E-Mail Removed)-halle.de...
> a wrote:
> > #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






 
Reply With Quote
 
Michele Dondi
Guest
Posts: n/a
 
      07-27-2007
On Fri, 27 Jul 2007 08:31:04 GMT, "a" <(E-Mail Removed)> wrote:

>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
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Regular Expression - looking to match 'www' only if it is the start of a URL hooterbite@yahoo.com ASP .Net 0 07-20-2005 04:11 PM
Regular Expression - looking to match 'www' only if it the start of a URL hooterbite@yahoo.com ASP .Net 4 07-12-2005 01:01 PM
how to match regular expression from right to left Liang Perl 2 08-27-2004 10:03 PM
match three digit number using regular expression championsleeper Perl 6 04-06-2004 08:54 PM
Dynamically changing the regular expression of Regular Expression validator VSK ASP .Net 2 08-24-2003 02:47 PM



Advertisments