Pattern-Matching var - interpolation.

Pattern-Matching var - interpolation.

Post by Angerste » Sat, 20 Jul 2002 17:11:25



Hello,
I have a very unfunny problem, on which i am working for hours.

I need to find out if a string contains an other specified string.
I want to use pattern-matching.
I cant get a match together which make this. Look at my examples,
none of the Matchings using vars works. If I replaces the vars to normal
strings it works like it should.
I am using perl 5.6 on a aix 4.3.3.

Thanks for help!
############################################ cut
#################################################
#!/usr/bin/perl -w

my $hund= "hund m-a-u-s katze k.a.t.z.e. maus xyz 1234";
my $dings1 = "hund";
my $dings2 = "m-a-u-s";
my $dings3 = "k.a.t.z.e.";
my $dings4 = "xyz";
my $dings5 = "123";

if ( $dings1 =~ /\Q$hund/) {
        print "$dings1 in \$hund";}

if ( $dings1 =~ /\Q${hund}/) {
        print "$dings1 in \$hund";}

if ( $dings3 =~ /${hund}/) {
        print "$dings3 in \$hund";}

if ( $dings3 =~ /\Q${hund}/) {
        print "$dings3 in \$hund";}

if ( $dings5 =~ /$hund/) {
        print "$dings5 in \$hund";}

if ( $dings1 =~ /\Q$hund/) {
        print "$dings1 in \$hund";}

if ( $dings1 =~ /.*\Q${hund}\E.*/) {
        print "$dings1 in \$hund";}

if ( $dings3 =~ /\${hund}/) {
        print "$dings3 in \$hund";}

if ( $dings3 =~ /.*\Q${hund}/) {
        print "$dings3 in \$hund";}

if ( $dings5 =~ /.*$hund.*/) {
        print "$dings5 in \$hund";}

if ( $dings4 =~ /.*$hund.*/) {
        print "$dings5 in \$hund";}
###################################### EOF
#############################################

 
 
 

Pattern-Matching var - interpolation.

Post by Sudarshan Raghav » Sat, 20 Jul 2002 17:34:12




> > Hello,
> > I have a very unfunny problem, on which i am working for hours.

> > I need to find out if a string contains an other specified string.
> > I want to use pattern-matching.
> > I cant get a match together which make this. Look at my examples,
> > none of the Matchings using vars works. If I replaces the vars to normal
> > strings it works like it should.
> > I am using perl 5.6 on a aix 4.3.3.

> > Thanks for help!
> > ############################################ cut
> > #################################################
> > #!/usr/bin/perl -w

> > my $hund= "hund m-a-u-s katze k.a.t.z.e. maus xyz 1234";
> > my $dings1 = "hund";
> > my $dings2 = "m-a-u-s";
> > my $dings3 = "k.a.t.z.e.";
> > my $dings4 = "xyz";
> > my $dings5 = "123";

> > if ( $dings1 =~ /\Q$hund/) {
> >       print "$dings1 in \$hund";}

> You are searching for $dings1 in $hund, this must be the other way around
> if ($hund =~ /\Q$hund/)

sorry, this should be if ($hund =~ /\Q$dings1/)

 
 
 

Pattern-Matching var - interpolation.

Post by Sudarshan Raghav » Sat, 20 Jul 2002 17:28:56



> Hello,
> I have a very unfunny problem, on which i am working for hours.

> I need to find out if a string contains an other specified string.
> I want to use pattern-matching.
> I cant get a match together which make this. Look at my examples,
> none of the Matchings using vars works. If I replaces the vars to normal
> strings it works like it should.
> I am using perl 5.6 on a aix 4.3.3.

> Thanks for help!
> ############################################ cut
> #################################################
> #!/usr/bin/perl -w

> my $hund= "hund m-a-u-s katze k.a.t.z.e. maus xyz 1234";
> my $dings1 = "hund";
> my $dings2 = "m-a-u-s";
> my $dings3 = "k.a.t.z.e.";
> my $dings4 = "xyz";
> my $dings5 = "123";

> if ( $dings1 =~ /\Q$hund/) {
>    print "$dings1 in \$hund";}

You are searching for $dings1 in $hund, this must be the other way around
if ($hund =~ /\Q$hund/)

You might also want to take a look at index (perldoc -f index).
Note, if you are searching for 'orld' in 'Hello World' the regex will
succeed. If you do not want this take look at '\b' in perldoc perlre

- Show quoted text -

Quote:

> if ( $dings1 =~ /\Q${hund}/) {
>    print "$dings1 in \$hund";}

> if ( $dings3 =~ /${hund}/) {
>    print "$dings3 in \$hund";}

> if ( $dings3 =~ /\Q${hund}/) {
>    print "$dings3 in \$hund";}

> if ( $dings5 =~ /$hund/) {
>    print "$dings5 in \$hund";}

> if ( $dings1 =~ /\Q$hund/) {
>    print "$dings1 in \$hund";}

> if ( $dings1 =~ /.*\Q${hund}\E.*/) {
>    print "$dings1 in \$hund";}

> if ( $dings3 =~ /\${hund}/) {
>    print "$dings3 in \$hund";}

> if ( $dings3 =~ /.*\Q${hund}/) {
>    print "$dings3 in \$hund";}

> if ( $dings5 =~ /.*$hund.*/) {
>    print "$dings5 in \$hund";}

> if ( $dings4 =~ /.*$hund.*/) {
>    print "$dings5 in \$hund";}
> ###################################### EOF
> #############################################

 
 
 

Pattern-Matching var - interpolation.

Post by Connie Ch » Sat, 20 Jul 2002 17:44:47


I suggest you better put all the "keywords" in array , then use a loop to
handle it...

my $hund= "hund m-a-u-s katze k.a.t.z.e. maus xyz 1234";

"123") ;


{   if ($hund =~ /$_/) { print "'$_' found at hund\n" }
     else { print "'$_' not found in Hund\n" } ;

Quote:}

Result :
'hund' found at hund
'Sudarshan' not found in Hund
'm-a-u-s' found at hund
'k.a.t.z.e.' found at hund
'xyz' found at hund
'Connie' not found in Hund
'123' found at hund

Rgds,
Connie

----- Original Message -----


Sent: Friday, July 19, 2002 4:28 PM
Subject: Re: Pattern-Matching var - interpolation.


> > Hello,
> > I have a very unfunny problem, on which i am working for hours.

> > I need to find out if a string contains an other specified string.
> > I want to use pattern-matching.
> > I can get a match together which make this. Look at my examples,
> > none of the Matchings using vars works. If I replaces the vars to normal
> > strings it works like it should.
> > I am using perl 5.6 on a aix 4.3.3.

> > Thanks for help!
> > ############################################ cut
> > #################################################
> > #!/usr/bin/perl -w

> > my $hund= "hund m-a-u-s katze k.a.t.z.e. maus xyz 1234";
> > my $dings1 = "hund";
> > my $dings2 = "m-a-u-s";
> > my $dings3 = "k.a.t.z.e.";
> > my $dings4 = "xyz";
> > my $dings5 = "123";

> > if ( $dings1 =~ /\Q$hund/) {
> > print "$dings1 in \$hund";}

> You are searching for $dings1 in $hund, this must be the other way around
> if ($hund =~ /\Q$hund/)

> You might also want to take a look at index (perldoc -f index).
> Note, if you are searching for 'orld' in 'Hello World' the regex will
> succeed. If you do not want this take look at '\b' in perldoc perlre

> > if ( $dings1 =~ /\Q${hund}/) {
> > print "$dings1 in \$hund";}

> > if ( $dings3 =~ /${hund}/) {
> > print "$dings3 in \$hund";}

> > if ( $dings3 =~ /\Q${hund}/) {
> > print "$dings3 in \$hund";}

> > if ( $dings5 =~ /$hund/) {
> > print "$dings5 in \$hund";}

> > if ( $dings1 =~ /\Q$hund/) {
> > print "$dings1 in \$hund";}

> > if ( $dings1 =~ /.*\Q${hund}\E.*/) {
> > print "$dings1 in \$hund";}

> > if ( $dings3 =~ /\${hund}/) {
> > print "$dings3 in \$hund";}

> > if ( $dings3 =~ /.*\Q${hund}/) {
> > print "$dings3 in \$hund";}

> > if ( $dings5 =~ /.*$hund.*/) {
> > print "$dings5 in \$hund";}

> > if ( $dings4 =~ /.*$hund.*/) {
> > print "$dings5 in \$hund";}
> > ###################################### EOF
> > #############################################

> --