Regexp containing plus-chars won't work?!?!? 
Author Message
 Regexp containing plus-chars won't work?!?!?

Hello,

I'm trying to find if a certain string ($foo)is found within another tring
($bar) using:

    if ($bar=~/$foo/){

but if the "$foo" contains plus-chars("+"), I get error message.

i.e.: What is wrong in the following:

=======================================================
#!/usr/bin/perl

$bar="1*abc;2*+++123;1*efg";
$foo="+++123";
print "[$bar]\n[$foo]\n";
if ($bar=~/$foo/){
        print "Found!\n";

Quote:
}else{

        print "Not found!\n";
Quote:
}

======================================================

Pekka Siiskonen



Wed, 18 Jun 1902 08:00:00 GMT  
 Regexp containing plus-chars won't work?!?!?

Quote:

> Hello,

> I'm trying to find if a certain string ($foo)is found within another tring
> ($bar) using:

>     if ($bar=~/$foo/){

> but if the "$foo" contains plus-chars("+"), I get error message.

> i.e.: What is wrong in the following:

> =======================================================
> #!/usr/bin/perl

> $bar="1*abc;2*+++123;1*efg";
> $foo="+++123";
> print "[$bar]\n[$foo]\n";
> if ($bar=~/$foo/){
>         print "Found!\n";
> }else{
>         print "Not found!\n";
> }

+ has special meaning in regular expressions.
First you should escape pluses and you have to use single quotes,
otherwise \+ interpolates.

$foo='\+\+\+123';
--

Ras Levrinc



Wed, 18 Jun 1902 08:00:00 GMT  
 Regexp containing plus-chars won't work?!?!?

Quote:

>+ has special meaning in regular expressions.
>First you should escape pluses and you have to use single quotes,
>otherwise \+ interpolates.

>$foo='\+\+\+123';

So: how should I test the matching? The $foo does contain the said plus
chars ('print "$foo"' and 'print $foo' prints the plus-chars on screen).
Contents of $foo and $bar come from databases in actual script. ????

pekka



Wed, 18 Jun 1902 08:00:00 GMT  
 Regexp containing plus-chars won't work?!?!?

Quote:

> Hello,

> I'm trying to find if a certain string ($foo)is found within another tring
> ($bar) using:

>     if ($bar=~/$foo/){

> but if the "$foo" contains plus-chars("+"), I get error message.

The contents of variables are interpolated before a regexp is compiled.
Thus,
$a = '(\w+).*'; "hi there"=~/\Q$a\E/;
will match and it will return "hi" in $1. It will not look for parens
backslash w plus etc.

To quote special strings, use the quotemeta function (perldoc -f
quotemeta) or embed the variable in \Q\E pairs

- Alex



Wed, 18 Jun 1902 08:00:00 GMT  
 Regexp containing plus-chars won't work?!?!?

Quote:
>[The problem was not about having difficulty in inserting the plus-chars
>into $foo -- they are there, if you try the small program, they will be
>printed -- but the RegExp didn't not work! This solution from Sebastien did
>the the trick!]
>>Replace /$foo/ with /\Q$foo\E/, this will stop all the special regex

There is absolutely no need to use a regular expression here at
all.  You're simply looking for a specific substring.  That /is/ why Perl
has the index() function.

  if (index($string, $substring) != -1) { $substring is found in $string }

--

http://www.pobox.com/~japhy/                  http://pinyaj.stu.rpi.edu/
PerlMonth - An Online Perl Magazine            http://www.perlmonth.com/
The Perl Archive - Articles, Forums, etc.    http://www.perlarchive.com/



Wed, 18 Jun 1902 08:00:00 GMT  
 Regexp containing plus-chars won't work?!?!?

Quote:


> > Hello,

> > I'm trying to find if a certain string ($foo)is found within another tring
> > ($bar) using:

> >     if ($bar=~/$foo/){

> > but if the "$foo" contains plus-chars("+"), I get error message.

> The contents of variables are interpolated before a regexp is compiled.
> Thus,
> $a = '(\w+).*'; "hi there"=~/\Q$a\E/;

DOH!
This was correctly
$a = '(\w+).*'; "hi there"=~/$a/;
before I changed the wrong text ;-(

- Show quoted text -

Quote:
> will match and it will return "hi" in $1. It will not look for parens
> backslash w plus etc.

> To quote special strings, use the quotemeta function (perldoc -f
> quotemeta) or embed the variable in \Q\E pairs

> - Alex



Wed, 18 Jun 1902 08:00:00 GMT  
 Regexp containing plus-chars won't work?!?!?
Hello,

Quote:

> Hello,

> I'm trying to find if a certain string ($foo)is found within another
tring
> ($bar) using:

>     if ($bar=~/$foo/){

Replace /$foo/ with /\Q$foo\E/, this will stop all the special regex
chars
from being processed and treated as the char they represent. See perlre
for details.

Cheers,
Sebastien.



Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. sort of array containing strings doesn't work

2. my gif's won't work after copying them

3. regexp: saving backreferences that won't overwrite existing values

4. substitution variable won't change in regexp

5. Detecting string containing no printing chars ?

6. convert a Perl AV* (containing strings) to a C char**

7. how to define alphanumeric (words containing non-ASCII chars)

8. Perl code that won't work with Embperl

9. www- why won't this work?!

10. Majordomo won't work with OSF/1 2.0

11. help? my cgi script won't work

 

 
Powered by phpBB® Forum Software