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

im trying to extract the last section of text from a string.. what im trying to do is very clear so here's the code i have:
my $endquote = 'PASS'; my $str = "NOTICE AUTH :*** Ident broken or disabled, to continue to c +onnect you must type /QUOTE PASS 16934"; print $str =~ /^$endquote(.*?)/;
this should produce 16934, right? yes its an irc bot.

Replies are listed 'Best First'.
Re: regex not working..
by davido (Cardinal) on May 12, 2012 at 15:41 UTC

    The metacharacter "^" anchors your match to the left side of the string. The characters from your target string found at the left side of the string are "NOTICE", not "PASS". It's impossible for your target string to match [lefthand side of string]PASS.

    Perhaps change it to:

    m{/QUOTE\sPASS\s(\S+)$}

    That anchors to "/QUOTE PASS ", and then captures any non-space characters through the end of the string. If it's permissible to have space characters embedded within the field, change it to:

    m{/QUOTE\sPASS\s(.+)$}

    And if the field is optional:

    m{/QUOTE\sPASS(?:\s(.+))?$}

    Dave

      Thanks.. now just having the problem that it wont accept any of these:
      if (index($input, "/QUOTE") != -1) { my $str = $input; ($pass) = $str =~ m{/QUOTE\sPASS\s(.*?)$}; print $sock "/QUOTE PASS $pass\r\n"; print $sock "QUOTE PASS $pass\r\n"; print $sock "/QUOTE PASS$pass\r\n"; sleep 2; print $sock "QUOTE PASS $pass\r\n"; print "found quote $pass\r\n"; }
      as you can see im trying everything heh.. this is for my channel bot, currently i dont have X so im using it to op me and other operators and also to auto ban on certain words
        ... it wont accept any of these ...

        Not sure what you mean by 'it' (since you do not supply a self-contained, runnable code example) or 'these' (since you don't supply the data in your strings), but your code (as well as all the possibilities suggested by davido) seems to work for me.

        >perl -wMstrict -le "my $str = 'NOTICE AUTH :*** Bla bla must type /QUOTE PASS 16934'; my $pass; ($pass) = $str =~ m{/QUOTE\sPASS\s(.*?)$}; print qq{'$pass'}; " '16934'