in reply to Re^3: How can I expand my substring?
in thread How can I expand my substring?

Oups, I think it misses case 4 or not? I mean, if the string starts with --- and doesn't have any NNNN before the BBB, then the BBB should be expanded only to the right (if there is space).

Replies are listed 'Best First'.
Re^5: How can I expand my substring?
by Corion (Patriarch) on Jun 30, 2014 at 12:17 UTC

    Ah, I misread that part of the specification.

    Most likely, the easiest approach is now to do replacement with /e and look-ahead and look-behind:

    # Expand string to the left $string4 =~ s!(?<=N)(-+)(?=B)!'B' x length $1!e; # Expand string to the right $string4 =~ s!(?<=B)(-+)!'B' x length $1!e;
      Now it works for all cases...
      If I am not overdoing it, could you please explain your syntax a bit? I am new to Perl and doesn't seem to grasp your substitution way here...

        The idea is to replace "N-B" by "NBB", where there can be more than one "-". This is done by using look-behind and look-ahead, which check that all the dashes are surrounded by N and B. The dashes are captured in $1 and then a string just as long as $1, but filled with "B" is replaced there.

        The same approach goes for the right side.

        There are perlre and YAPE::Regex::Explain:

        >perl -MYAPE::Regex::Explain -e "print YAPE::Regex::Explain->new(shift +)->explain" "(?<=N)(-+)(?=B)" The regular expression: (?-imsx:(?<=N)(-+)(?=B)) matches as follows: NODE EXPLANATION ---------------------------------------------------------------------- (?-imsx: group, but do not capture (case-sensitive) (with ^ and $ matching normally) (with . not matching \n) (matching whitespace and # normally): ---------------------------------------------------------------------- (?<= look behind to see if there is: ---------------------------------------------------------------------- N 'N' ---------------------------------------------------------------------- ) end of look-behind ---------------------------------------------------------------------- ( group and capture to \1: ---------------------------------------------------------------------- -+ '-' (1 or more times (matching the most amount possible)) ---------------------------------------------------------------------- ) end of \1 ---------------------------------------------------------------------- (?= look ahead to see if there is: ---------------------------------------------------------------------- B 'B' ---------------------------------------------------------------------- ) end of look-ahead ---------------------------------------------------------------------- ) end of grouping ----------------------------------------------------------------------
        and
        The regular expression: (?-imsx:(?<=B)(-+)) matches as follows: NODE EXPLANATION ---------------------------------------------------------------------- (?-imsx: group, but do not capture (case-sensitive) (with ^ and $ matching normally) (with . not matching \n) (matching whitespace and # normally): ---------------------------------------------------------------------- (?<= look behind to see if there is: ---------------------------------------------------------------------- B 'B' ---------------------------------------------------------------------- ) end of look-behind ---------------------------------------------------------------------- ( group and capture to \1: ---------------------------------------------------------------------- -+ '-' (1 or more times (matching the most amount possible)) ---------------------------------------------------------------------- ) end of \1 ---------------------------------------------------------------------- ) end of grouping ----------------------------------------------------------------------
      Hi there!
      I was wondering, how can we modify the regexp so as the "expansion" of the BBB label is done for a maximum of 200 positions to the left and to the right (and not till we reach the N label)?

        You could play around with the length $1 expression and try to limit that, but I'm not sure what corner cases would be relevant.

        See perlop on how s///e works.