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

Looking at code from Abigail-II's earlier post, I am curious as to why the following doesn't do what I think it does:
#!/usr/bin/perl use strict; use warnings; while (<DATA>) { $_ =~ s/[(]*(?![)]+)//; $_ =~ s/[)]*(?<![(])//; print $_, "\n"; } __DATA__ (1.3.56.84 56.38.m.26) 56.2.3.(59)
$_ =~ s/[(]*(?![)]+)//; # Replace 0 or more instances of '(' if the # lookahead assertion for one or more of ')' # fails. This seems to work correctly $_ =~ s/[)]*(?<![(])//; # Replace 0 or more instances of ')' if the # lookbehind assertion for '(' fails. This # doesn't work as I though.
Any help would be appreciated

Replies are listed 'Best First'.
Re: Extended Regex Sequences
by TomDLux (Vicar) on Nov 06, 2003 at 16:13 UTC

    If you remove the star, the second regex works; if you change it to a plus, and modify the data to have multiple closing brackets, it still works.

    The problem is that since the regex can match on zero closing brackets, it does so at the beginning of the string. Since the previous character is not an opening bracket, the search portion succeeds, and the replace portion replaces the empty match with an empty string. If you modify the second datum to begin with a closing bracket, you'll see that it gets edited the way you wanted.

    If you change the regex to operate globally, you'll get the effect I think you want. Add a 'g' at the end:

    $_ =~ s/[)]*(?<![(])//g;

    --
    TTTATCGGTCGTTATATAGATGTTTGCA

Re: Extended Regex Sequences
by jryan (Vicar) on Nov 06, 2003 at 16:01 UTC

    Could you explain how you expect the 2nd regex to work? It shouldn't do anything; it will fail if it ever matches a '(' [first match a series of ')', and yet the last character matched from this sequence can't be a '(' - think about it, its impossible]; it will only succeed if 0 '(' are matched, in which case the regex will match a 0-length string, and replace nothing.

Re: Extended Regex Sequences
by Anonymous Monk on Nov 06, 2003 at 16:41 UTC
    I would expect it to swap:
    (1.3.56.84 56.38.m.26) 56.2.3.(59) TO: 1.3.56.84 56.38.m.26 56.2.3.(59)
    Removing only single parens