I am trying to find patterns in a 2D matrix represented as a string. Imagine the following:
// horizontal line String pat1 = "............." + "............." + "............." + "....XXXX....." + "............." + "............."; // vertical line String pat2 = "............." + "......X......" + "......X......" + "......X......" + "......X......" + ".............";
Finding the first pattern will be trivial, the regular expression will look something like this:
X+
In the second case, this is a little more complicated, but doable, since I know the number of columns and rows of the matrix:
(X.{`WIDTH - 1`})+
When I ran into problems with the correct regular expression, we tried to figure out a way to recognize the following patterns:
// fixed but unknown number of columns String pat3 = "............." + ".....XXX....." + ".....XXX....." + ".....XXX....." + ".....XXX....." + "............."; // variable number of columns String pat4 = "............." + ".....XXX....." + "....XXXXX...." + "...XXXXXXX..." + ".....XXX....." + ".............";
I am looking for a way to create a regex pattern equivalent to:
(X.{`WIDTH - PREVCOUNT`})+
Where PREVCOUNT is the length of the last matched pattern (I know that I missed the first X of the fourth line in pat4, but I can live with it). I know there are regular expressions in a regular expression, but I wonder if what I am trying to achieve is possible. Even if it was possible, I also worry about the performance associated with using lookaheads, as I do not quite understand how they work internally.
Is there a way to do this with a single regex check, or do I need to search line by line and then try to check if all X are adjacent?
Edit: As an explanation, I'm trying to find the “drops” of X. As long as there is a continuous X in columns / rows, it can be considered to belong to blob. A few examples:
String blob1 = "............." + "......XX....." + "....XXXX....." + "...XXXXX....." + ".....XXX....." + "............."; String blob2 = "............." + ".....XXX....." + "....XXXXX....." + "...XXXXXXX..." + "....XXXXX...." + ".....XXX....."; String blob3 = "............." + ".....XXX....." + ".....XXX......" + ".....XXX....." + "............." + "............."; String notblob = "............." + "..XXX........" + "......XXX....." + "..XXX........." + ".............." + ".............";
My solution doesn't have to be accurate, so I'm trying to use a lousy regex approach.