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

Hi

$line = "API##"; if($line =~ m/"API#"/) { print "Match found\n"; } else { print "Match not found\n"; }

Output Expected: "API##" not matched

I need to find the words with a single '#' but it shows matched. Pls help

-- TIA

Replies are listed 'Best First'.
Re: String Match
by moritz (Cardinal) on Nov 30, 2009 at 12:30 UTC
    If you want exact comparison, don't use regexes:
    if ($line eq "API#") { ... }

    If you want to use regexes, maybe you need some anchors. See perlretut.

    Also the double quotes inside the regex are a bit confusing - you won't get any match with them, unless your line also contains literal double quotes.

    Perl 6 - links to (nearly) everything that is Perl 6.
Re: String Match
by bellaire (Hermit) on Nov 30, 2009 at 12:28 UTC
    You don't need quotes inside the regex (the paired slashes do that job), including them searches for quotes inside your string, which you don't have. That makes:
    if($line =~ m/API#/) { ...
    UPDATE: Thanks for the replies, as ww indicates, you actually want to negate the search on the second # symbol, as his solution does (see below).
      Almost.
      if($line =~ m/API#[^#]/) { ...

      The negative character class makes the regex reject the doubled-#, as per OP's question.

      Update: Better answer below at Re^3: String Match. ++ happy.barney!

        better is to use negative look around assertion: m/API#(?!#)/, it match also end of line.
      This din't solve the problem. It says "Matched".