jayd11 has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks, need your inputs on the below scenario I am trying to convert sql like operation on perl regular expression I have to compare two variable strings , one string is coming as sql like operator (%) 3 scenarios -

foo% ( similar in perl ^foo+ ) %foo% ( similar in perl foo +) %f%o% ( no idea on perl)
I am not able to achieve this in perl , can someone help on this code
$find1='%'; if ($field2_b ne "") { s/[^$find1]?^/\^/,s/[$find1]?$/+/ for $field2_b + }; if ($field1 =~ /$field2_b/i) { print "fields have pattern matching \n" +;
Thanks,

Replies are listed 'Best First'.
Re: Need to append and replace for % sign in a variable string
by Corion (Patriarch) on May 24, 2015 at 07:25 UTC

    Have you actually tested your string replacements? If you look at perlre, you will find that ^foo+ will only match foo, fooo and so on, but never foobar.

    Please show a self-contained short example that demonstrates your problem. The code as you posted it does not work, so we can't really help you there.

    It might be almost enough to just replace every % with (?:.*), but you will still need to understand how to deal with an SQL string that already contains * for example.

Re: Need to append and replace for % sign in a variable string
by AnomalousMonk (Archbishop) on May 24, 2015 at 07:42 UTC

    I do not understand your overall question.

    However, some specific comments on the OPed regexes:

    • Inside and at the very beginning of a character class, the  ^ regex metacharacter produces class "inversion", i.e., the class matches any character not in the class. So  [^$find1] is the same as  [^%] and matches any character that is not a  '%' character.
    • Outside a character class, the  ^ metacharacter asserts the beginning of the string (if the  /m regex modifier is not used). So  [^$find1]?^ is the same as  [^%]?^ and matches zero or one of any character that is not a  '%' and that is before the beginning of the string. Effectively, it matches the beginning of the string and a  '^' character is always inserted at the beginning of the string by the substitution.
    • The expression  [$find1]?$ matches zero or one  '%' characters at the end of the string (or before a newline at the end of the string). Effectively, a  '+' character is always inserted at the end of the string by the substitution.

    Update:

    c:\@Work\Perl\monks>perl -wMstrict -le "my $field2_b = 'abcd'; ;; my $find1 = '%'; if ($field2_b ne '') { s/[^$find1]?^/\^/, s/[$find1]?$/+/ for $field2 +_b }; print qq{'$field2_b'}; " '^abcd+'
    Please see perlre, perlrecharclass, perlrequick, and especially perlretut.


    Give a man a fish:  <%-(-(-(-<

Re: Need to append and replace for % sign in a variable string ( Regexp::Wildcards->new( type => 'sql')
by Anonymous Monk on May 24, 2015 at 07:28 UTC
    perlintro, Regexp::Wildcards, Modern Perl a loose description of how experienced and effective Perl 5 programmers work....You can learn this too.
    #!/usr/bin/perl -- use strict; use warnings; use Data::Dump qw/ dd /; Main( @ARGV ); exit( 0 ); sub Main { MainOne(); MainToo(); } sub MainOne { my @fields = ( 'I foo', 'foo who?', 'the foo and the moo' ); my @querys = qw/ foo% %foo% %f%o% /; for my $field ( @fields ) { for my $search ( @querys ) { my $re = WildcardRe( $search ); if( $field =~ m{$re}i ) { dd( { andthe => $field, has => $search, says => $re } +); } } } } ## end sub Main sub WildcardRe { ## todo update with Regexp::Wildcards my( $search ) = @_; my @parts = split /%/, $search; $_ = quotemeta( $_ ) for @parts; my $re = join '.+', @parts; $re = '^' . $re . '$'; return $re; } ## end sub WildcardRe sub MainToo { my @fields = ( 'I foo', 'foo who?', 'the foo and the moo' ); my @querys = qw/ foo% %foo% %f%o% /; @querys = map { [ $_, WildcardRe( $_ ) ]; } @querys; for my $field ( @fields ) { for my $query ( @querys ) { my( $search, $re ) = @$query; if( $field =~ m{$re}i ) { dd( { andthe => $field, has => $search, says => $re } +); } } } } ## end sub MainToo

    ppi_dumper can tell you what manual to look up the basic perl syntax in ... like perlsyn ... example of this Re: use constant usage clarification (Deparse)

      My understanding of % in SQL is that is corresponds to .* not .+.