regular expression match on the fly

Discussion in 'Perl Misc' started by a, Jul 27, 2007.

  1. a

    a Guest

    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
    a, Jul 27, 2007
    #1
    1. Advertising

  2. "a" <> 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
    Peter Makholm, Jul 27, 2007
    #2
    1. Advertising

  3. On Fri, 27 Jul 2007 06:27:36 GMT, "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?


    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,
    Michele Dondi, Jul 27, 2007
    #3
  4. a

    Mirco Wahab Guest

    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
    Mirco Wahab, Jul 27, 2007
    #4
  5. a

    a Guest

    "Mirco Wahab" <>
    ???????:f8c91m$l8j$-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
    a, Jul 27, 2007
    #5
  6. On Fri, 27 Jul 2007 08:31:04 GMT, "a" <> 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,
    Michele Dondi, Jul 27, 2007
    #6
    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. championsleeper
    Replies:
    6
    Views:
    1,003
    championsleeper
    Apr 6, 2004
  2. Liang
    Replies:
    2
    Views:
    1,676
  3. VSK
    Replies:
    2
    Views:
    2,287
  4. Replies:
    4
    Views:
    729
  5. Brian

    Fly outmenu on the fly

    Brian, Apr 8, 2005, in forum: Javascript
    Replies:
    0
    Views:
    103
    Brian
    Apr 8, 2005
Loading...

Share This Page