When you want do things such as working with multiline strings that represent a board (or a matrix or whatever), the best way is to unfold the string.
Either by removing the \n, and printing the line
with something like:
s/(.{$L})/$1\n/sg
where $L is the wanted length; or by putting
the whole board in a single string and using the \n
as line delimiters.
Here I use the latter solution. You can use two regex,
if your input is indeed a matrix (i.e. all lines are of
the same length). First you can work the 'Y' columns, and
then the 'x' line:
# replace whatever is at columns 3,4, 8 and 9 by 'Y' s/^(...)..(...)..(...)$/$1YY$2YY$3/mg; # set the first line to 'xx' s/\G(x*)./$1x/gm;
There are more than one way to do this. What is it that you specifically want to do? Replace all vertical 4's by Y's or what?
Tips: /m let ^ and $ match the beginning and the ending of a line (on a multiline string). \A and \Z match the beginnning and ending of the string. I also used the fact that . doesn't match a newline, so the second regex cannot match further than the first line.
I am not really sure it's smarter than transposition, though...
In reply to RE: vertical regex in a matrix?
by BooK
in thread vertical regex in a matrix?
by jimt
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |