in reply to Re: Quick Regex trouble
in thread Quick Regex trouble

NOTEI think it's actually look-behind assertion he needs.

($var =~/(?<=use )neighbor[[:alpha:]-]* ([[:alnum:]\.-:]+)/) and do{ print $1; };
output 2001:504:0:4::6181:1 If that is the output the OP wanted.

Because with "negative look-behind assertion", the match is not successful (failed). In fact, there is no output.
Checked with use re 'debug';
.. produces ..
Compiling REx `(?<!use )neighbor[[:alpha:]-]* ([[:alnum:]\.-:]+)' size 42 Got 340 bytes for offset annotations. first at 1 1: UNLESSM[-4](7) 3: EXACT <use >(5) 5: SUCCEED(0) 6: TAIL(7) 7: EXACT <neighbor>(10) 10: STAR(23) 11: ANYOF[\-A-Za-z+utf8::IsAlpha](0) 23: EXACT < >(25) 25: OPEN1(27) 27: PLUS(40) 28: ANYOF[.-:A-Za-z+utf8::IsAlnum](0) 40: CLOSE1(42) 42: END(0) anchored `neighbor' at 0 floating ` ' at 8..2147483647 (checking ancho +red) minlen 10 Offsets: [42] 8[4] 0[0] 5[4] 0[0] 8[0] 8[0] 10[8] 0[0] 0[0] 30[12] 18[12] 0[0] 0 +[0] 0[0] 0[0] 0[0] 0[0] 0[0] 0[0] 0[0] 0[0] 0[0] 31[1] 0[0] 32[1] 0[0 +] 48[15] 33[15] 0[0] 0[0] 0[0] 0[0] 0[0] 0[0] 0[0] 0[0] 0[0] 0[0] 0[0 +] 49[1] 0[0] 50[0] Guessing start of match, REx `(?<!use )neighbor[[:alpha:]-]* ([[:alnum +:]\.-:]+)' against `use neighbor 2001:504:0:4::6181:1'... Found anchored substr `neighbor' at offset 4... Found floating substr ` ' at offset 12... Starting position does not contradict /^/m... Guessed: match at offset 4 Matching REx `(?<!use )neighbor[[:alpha:]-]* ([[:alnum:]\.-:]+)' again +st `neighbor 2001:504:0:4::6181:1' Setting an EVAL scope, savestack=5 4 <use > <neighbor> | 1: UNLESSM[-4] 0 <> <use neighbor> | 3: EXACT <use > 4 <use > <neighbor> | 5: SUCCEED could match... failed... Match failed Freeing REx: `"(?<!use )neighbor[[:alpha:]-]* ([[:alnum:]\\.-:]+)"'


UPDATE:
NOTE: I think I got this wrong, and kennethk was right! Since, the OP gave a condition on which the regex should match as "when the string doesn't have USE" and the sample he gave has it.

If you tell me, I'll forget.
If you show me, I'll remember.
if you involve me, I'll understand.
--- Author unknown to me

Replies are listed 'Best First'.
Re^3: Quick Regex trouble
by Laurent_R (Canon) on Jul 16, 2013 at 21:28 UTC

    I think it's actually look-behind assertion he needs.

    I don't think so. The OP said that it should match "neighbor" but not "use neighbor", so that a zero-width negative look-behind assertion seems to be what is needed to exclude "use".

    Just an additional note: the do {} block is unnecessary in the OP code. The solution could be:

    ($var =~/(?<!use )neighbor[[:alpha:]-]* ([[:alnum:]\.-:]+)/) and print $1;

    or even:

    print $1 if $var =~/(?<!use )neighbor[[:alpha:]-]* ([\d:]+)/;