in reply to regex: something...(!something)...something

I think you've basically got it right.

/^somebegin(?!something)someend$/

Should work. The only complexity involves lookahead vs. backtrack and because you have stuff both before and after the stuff you don't want, that won't matter (in terms of the truth of the statement, I have no idea about the efficiency).

Personally, if I'm confused by something like this I opt for the slow but readable...

if ( /^somebegin(.*)someend$/ ) { my $middle = $1; if ( $middle !~ /^something$/ ) { # woot } }

Replies are listed 'Best First'.
Re^2: regex: something...(!something)...something
by aaaone (Initiate) on Jul 17, 2008 at 17:31 UTC
    hey! it is really not flexible. I'll give more complex question: there is some html.
    <tr><td>1</td><td>2</td><td>3</td><td>4</td><tr> OR with <font> - the task is that it cannot be or there can be somethi +ng else <tr><td>1</td><td>2</td><td><font>3</font></td><td>4</td><tr>
    I need to do something like this:
    replace <td>..(!<td>)..3..(!</td>)..</td> with <td>(everything that was in the left middle before 3)TEXT(everyth +ing that was in the right middle after 3)</td>
    Hope, you understood me..

      For this, I'd use instead:

      use strict; use warnings; my $text="<tr><td>1</td><td>2</td><td>qw<font>3</font></td><td>4</td>< +tr>"; my @blocks = split /<td>(.*?)<\/td>/, $text; foreach my $block (@blocks) { if($block =~ /3/) { print $block."\n"; } }

      outputs

      qw<font>3</font>

      But you should consider one of the many HTML parser modules.

      hm.. I see this looks like ugly but working solution and with some modifications I'll use it... but it is so sad there is no any commmon regex to solve the problem (as I wrote in some other post, such problems I met not only with HTML..)

        I personally believe that you're actually getting quite repetitive: the fact that you have admittedly "too small a knowledge" but nevertheless think that "there should be a regex solution" is in conflict with the fact that virtually everybody who is knowledgeable knows that they're poor enough for HTML parsing. And if it's not strictly to do only with HTML parsing, but is of comparable complexity, then they are still just as poor and a good enough solution may be given in terms of code comprising e.g. several regexen and other logic. Point is, regexen are powerful, and have been capable of matching stuff that is far from being "regular" for a long time, but there's some common stuff which in turn is still too irregular for them. It is my understanding that Perl 6's rules will be powerful enough to parse HTML (and some other cool things the most famous of which is Perl 6 itself!) Then they should be able to parse your other thingy, whatever it is. What's more, they will be able to do so in a very clear and readable manner, organized in grammars. Up to then, if really wanting to, you may indeed be able to have at your hands some fraction of their power in Perl 5's regexen, which may be enough or not, depending on the actual situation. But in your real world case, I suspect the solution would come out either severely unreliable or horribly looking, with various intermediate degrees between such extremes.

        --
        If you can't understand the incipit, then please check the IPB Campaign.