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:
and>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 ----------------------------------------------------------------------
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 ----------------------------------------------------------------------
In reply to Re^7: How can I expand my substring?
by Corion
in thread How can I expand my substring?
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |