in reply to Negating a leading whitespace

icg,
If I understand correctly, you want to do something with a line if it contains 2 characters but exclude it if they are preceded by a whitespace. Negated character classes to the rescue:
if ( $line =~ /[^\s]XY/ ) { ... }
Where XY are the two characters you want. Alternatively, if the whitespace requirement for exclusion is at the beginning of the line you can just do:
while ( <FILE> ) { next if /^\s/; # process lines without leading whitespace here ... }

Cheers - L~R

Replies are listed 'Best First'.
Re^2: Negating a leading whitespace
by Roy Johnson (Monsignor) on Jul 05, 2005 at 15:23 UTC
    Your first example wouldn't match a line that began with XY, because it requires some character to precede it. Negative lookbehind or an alternation would fix:
    /(?:^|\S)XY/ # or /(?<!\s)XY/
    (I also prefer the builtin not-whitespace char, rather than negating the class).

    Caution: Contents may have been coded under pressure.
Re^2: Negating a leading whitespace
by davidrw (Prior) on Jul 05, 2005 at 15:30 UTC
    I believe OP wants to check for the 2 chars _anywhere_ in the string, as long as the line doesn't start w/whitespace (so your second method)... I think this is easiest w/just two checks (i was having trouble making a single regex w/look-behinds):
    if ( $line =~ /^\S/ && $line =~ /XY/ ){ ... }
    Unless you can assume that the 2 chars will never be at the beginning of the line, in which case you can just do:
    if ( $line =~ /^\S.*?XY/ ){ ... }