in reply to perlre inverse check for several patterns

That looks like HTML. Don't parse HTML with regex, that way lies madness.

OK, with that out of the way, your match fails because the lookahead doesn't reset the pos. Include the right angle bracket and you're good to go.

use strict; use warnings; use Test::More tests => 1; my $str = 'xxx<pre>xxx<www>xxx<strong>xxx'; like $str, qr/<(?!strong>)/, "Valid tag found";

But again, don't do this. Use an HTML parser. You'll thank me later. :-)


🦛

Replies are listed 'Best First'.
Re^2: perlre inverse check for several patterns
by averlon (Sexton) on Jun 02, 2023 at 14:47 UTC

    Hi hippo!

    no, it is not HTML. It is some interface using some formatting strings like (!!like!!) HTML. But unfotunately the interface crashes if some "<>" strings are included which do not match the allowed formatting strings.

    The strings I process are lines from logfiles. Unfortunately some of these lines include "<xxx>" strings. This brings the interface I use into trouble. So I need to filter them out.

    I meanwhile found out I get a "true" if I use the following code:

    $av_tmp_STRING = "xxx<pre>xxx<www>xxx<strong>xxx"; if ( $av_tmp_STRING =~ m/<(?!strong>)(?!pre)/ ) { #do something with the string which contains wrong patterns }

    Still testing if it really works

    But anyhow. I will keep the example in mind for other use!

    Thanks

    Regards Kallewirsch
      no, it is not HTML. It is some interface using some formatting strings like (!!like!!) HTML. But unfotunately the interface crashes if some "<>" strings are included which do not match the allowed formatting strings.

      Could you enlighten us as to what exactly this format is and perhaps provide a more representative sample? Also, is it not feasible to fix the crashes in the interface?

        Hi haukex,

        in my private environment I follow some logfiles by File::Tail and report lines of interest to some TELEGRAM Channel via WWW::Telegram::BotAPI. The TELEGRAM API knows some forms of limited formatting of the string to send. One of the formatting options is called "HTML", although there are only 3 or 4 fomatting tags similar to the HTML tags.

        One e.g. is:

        <strong>xxx</strong>

        The example I gave is very much the same as a real logfile line. But if you wish I can give a real example:

        $av_tmp_LINE = "Jun 3 23:20:05 f42252s5 postfix/pickup[204714]: E1E63 +A045C: uid=33 from=<www-data>" $av_tmp_STRING = "Logfile: " . "<strong>" . $av_obj_TMP->{input} . "</ +strong>" . " " . $av_tmp_LINE;

        Now the string $av_tmp_STRING should be sent to the TELEGRAM Channel using "HTML" formatting. But unfotunately inside the string there is the "<www-data>" which looks like a formatting string, but is unsupported.

        This causes the module to abort, although eval is used.

        Messge:

        "Request failed with error 'ERROR: code 400: Bad Request: can't parse +entities: Unsupported start tag "www" at byte offset 53 at /usr/share +/perl5/WWW/Telegram/BotAPI.pm line 224. WWW::Telegram::BotAPI::api_re +quest(undef, undef, HASH(0x55e57ea8da98)) called at ./av_perlre.pl li +ne 268 eval {...} called at ./av_perlre.pl line 266 ', but I'm still +alive! at ./av_perlre.pl line 266."

        Since I am not a very experience programmer, I probably have done something wrong. I have already reported the issue to the module creator.

        My Code:

        $av_std_RETVAL = eval { $av_obj_TGRAM->sendMessage ( { chat_id => $av_loc_tgram_CHATID, text => $av_tmp_STRING, disable_notification => 'false', parse_mode => 'HTML', } ) } or warn "Request failed with error '$@', but I'm still alive!";

        This is according to the docu. The docu says, by using eval the way above, the program will not abort - but it does.

        Regards Kallewirsch
      > it is not HTML. It is some interface using some formatting strings like (!!like!!) HTML. But unfotunately the interface crashes if some "<>" strings are included which do not match the allowed formatting strings.

      So what's wrong with tybalt89's approach?

      see Re: perlre inverse check for several patterns

      Cheers Rolf
      (addicted to the 𐍀𐌴𐍂𐌻 Programming Language :)
      Wikisyntax for the Monastery