Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

RE: vertical regex in a matrix?

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


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
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2025-06-19 13:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.