Beefy Boxes and Bandwidth Generously Provided by pair Networks vroom
laziness, impatience, and hubris
 
PerlMonks  

RE: vertical regex in a matrix?

by BooK (Curate)
on Sep 11, 2000 at 14:36 UTC ( [id://31886]=note: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.


in reply to vertical regex in a matrix?

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://31886]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.