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

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...

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

    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!