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

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;

Replies are listed 'Best First'.
Re^6: How can I expand my substring?
by Anonymous Monk on Jun 30, 2014 at 12:24 UTC
    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 ----------------------------------------------------------------------
        Thanks for all your time! Especially the last part is the most important I think, since I can read up on it!
        Thanks again!
Re^6: How can I expand my substring?
by Anonymous Monk on Jul 22, 2014 at 13:04 UTC
    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.

        Thanks again, is it possible to break your code down a bit?
        Like in using more lines I mean, so I can then use and if clause in limiting the length $1 as you suggest...