I am trying to search for patterns in a 2D matrix represented as a string. Picture the following:
// horizontal line
String pat1 =
    "............." +
    "............." +
    "............." +
    "....XXXX....." +
    "............." +
    ".............";
// vertical line
String pat2 =
    "............." +
    "......X......" +
    "......X......" +
    "......X......" +
    "......X......" +
    ".............";
Searching for the first pattern would be trivial, the regex would be something like:
X+
In the second case, it is a little trickier but doable since I know the number of columns and rows of the matrix:
(X.{`WIDTH - 1`})+
When I ran into problems to come up with the correct regex was while trying 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....." +
    ".............";
What I am looking for is a way to create a regex pattern equivalent to:
(X.{`WIDTH - PREVCOUNT`})+
Where PREVCOUNT is the length of the  last matched pattern (I am aware that I would be missing the first X of the 4th line in pat4, but I can live with that). I know that there are lookaheads in regex, but I wonder if what I am trying to achieve is possible at all. Even if it was possible, I also worry about the performance hit of using lookaheads since I don't fully understand how they work internally.
Is there a way of doing this with a single regex validation, or do I have to search row by row and then try to see if the X's are all contiguous?
Edit: As a clarification, I am trying to search for "blobs" of X's. As long as there are contiguous X's across columns/rows it can be considered as belonging to a 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 does not need to be exact, hence why I am trying to use a probably lousy regex approach.
 
     
    