laziness, impatience, and hubris PerlMonks

### comment on

 Need Help??

I wrote a program that builds a regexp to solves Sudoku puzzles. The generated regexp is hidden underneath the "readmore" below.

Update: The generated regexp is too big to fit in a post. An abridged version was used.

```sub valid {
my \$spot = substr(\$grid_h, \$_[0]*\$size+\$_[1], 1);
return 1 if \$spot eq \$_[2];
return   if \$spot ne '_';

return if index(substr(\$grid_h, \$_[0] * \$size, \$size), \$_[2]) >= 0;
return if index(substr(\$grid_v, \$_[1] * \$size, \$size), \$_[2]) >= 0;

return 1;
}

\$re = qr/
(?:
(?(?{ !valid(0, 0, '1') })x)(?{ local \$grid_h = \$grid_h; substr(\$gr
+id_h, 0, 1, '1'); local \$grid_v = \$grid_v; substr(\$grid_v, 0, 1, '1')
+; }) |
(?(?{ !valid(0, 0, '2') })x)(?{ local \$grid_h = \$grid_h; substr(\$gr
+id_h, 0, 1, '2'); local \$grid_v = \$grid_v; substr(\$grid_v, 0, 1, '2')
+; }) |
(?(?{ !valid(0, 0, '3') })x)(?{ local \$grid_h = \$grid_h; substr(\$gr
+id_h, 0, 1, '3'); local \$grid_v = \$grid_v; substr(\$grid_v, 0, 1, '3')
+; }) |
(?(?{ !valid(0, 0, '4') })x)(?{ local \$grid_h = \$grid_h; substr(\$gr
+id_h, 0, 1, '4'); local \$grid_v = \$grid_v; substr(\$grid_v, 0, 1, '4')
+; }) |
(?(?{ !valid(0, 0, '5') })x)(?{ local \$grid_h = \$grid_h; substr(\$gr
+id_h, 0, 1, '5'); local \$grid_v = \$grid_v; substr(\$grid_v, 0, 1, '5')
+; }) |
(?(?{ !valid(0, 0, '6') })x)(?{ local \$grid_h = \$grid_h; substr(\$gr
+id_h, 0, 1, '6'); local \$grid_v = \$grid_v; substr(\$grid_v, 0, 1, '6')
+; }) |
(?(?{ !valid(0, 0, '7') })x)(?{ local \$grid_h = \$grid_h; substr(\$gr
+id_h, 0, 1, '7'); local \$grid_v = \$grid_v; substr(\$grid_v, 0, 1, '7')
+; }) |
(?(?{ !valid(0, 0, '8') })x)(?{ local \$grid_h = \$grid_h; substr(\$gr
+id_h, 0, 1, '8'); local \$grid_v = \$grid_v; substr(\$grid_v, 0, 1, '8')
+; }) |
(?(?{ !valid(0, 0, '9') })x)(?{ local \$grid_h = \$grid_h; substr(\$gr
+id_h, 0, 1, '9'); local \$grid_v = \$grid_v; substr(\$grid_v, 0, 1, '9')
+; })
)
(?:
(?(?{ !valid(0, 1, '1') })x)(?{ local \$grid_h = \$grid_h; substr(\$gr
+id_h, 1, 1, '1'); local \$grid_v = \$grid_v; substr(\$grid_v, 9, 1, '1')
+; }) |
(?(?{ !valid(0, 1, '2') })x)(?{ local \$grid_h = \$grid_h; substr(\$gr
+id_h, 1, 1, '2'); local \$grid_v = \$grid_v; substr(\$grid_v, 9, 1, '2')
+; }) |
(?(?{ !valid(0, 1, '3') })x)(?{ local \$grid_h = \$grid_h; substr(\$gr
+id_h, 1, 1, '3'); local \$grid_v = \$grid_v; substr(\$grid_v, 9, 1, '3')
+; }) |
(?(?{ !valid(0, 1, '4') })x)(?{ local \$grid_h = \$grid_h; substr(\$gr
+id_h, 1, 1, '4'); local \$grid_v = \$grid_v; substr(\$grid_v, 9, 1, '4')
+; }) |
(?(?{ !valid(0, 1, '5') })x)(?{ local \$grid_h = \$grid_h; substr(\$gr
+id_h, 1, 1, '5'); local \$grid_v = \$grid_v; substr(\$grid_v, 9, 1, '5')
+; }) |
(?(?{ !valid(0, 1, '6') })x)(?{ local \$grid_h = \$grid_h; substr(\$gr
+id_h, 1, 1, '6'); local \$grid_v = \$grid_v; substr(\$grid_v, 9, 1, '6')
+; }) |
(?(?{ !valid(0, 1, '7') })x)(?{ local \$grid_h = \$grid_h; substr(\$gr
+id_h, 1, 1, '7'); local \$grid_v = \$grid_v; substr(\$grid_v, 9, 1, '7')
+; }) |
(?(?{ !valid(0, 1, '8') })x)(?{ local \$grid_h = \$grid_h; substr(\$gr
+id_h, 1, 1, '8'); local \$grid_v = \$grid_v; substr(\$grid_v, 9, 1, '8')
+; }) |
(?(?{ !valid(0, 1, '9') })x)(?{ local \$grid_h = \$grid_h; substr(\$gr
+id_h, 1, 1, '9'); local \$grid_v = \$grid_v; substr(\$grid_v, 9, 1, '9')
+; })
)
(?:
(?(?{ !valid(0, 2, '1') })x)(?{ local \$grid_h = \$grid_h; substr(\$gr
+id_h, 2, 1, '1'); local \$grid_v = \$grid_v; substr(\$grid_v, 18, 1, '1'
+); }) |
(?(?{ !valid(0, 2, '2') })x)(?{ local \$grid_h = \$grid_h; substr(\$gr
+id_h, 2, 1, '2'); local \$grid_v = \$grid_v; substr(\$grid_v, 18, 1, '2'
+); }) |
(?(?{ !valid(0, 2, '3') })x)(?{ local \$grid_h = \$grid_h; substr(\$gr
+id_h, 2, 1, '3'); local \$grid_v = \$grid_v; substr(\$grid_v, 18, 1, '3'
+); }) |
(?(?{ !valid(0, 2, '4') })x)(?{ local \$grid_h = \$grid_h; substr(\$gr
+id_h, 2, 1, '4'); local \$grid_v = \$grid_v; substr(\$grid_v, 18, 1, '4'
+); }) |
(?(?{ !valid(0, 2, '5') })x)(?{ local \$grid_h = \$grid_h; substr(\$gr
+id_h, 2, 1, '5'); local \$grid_v = \$grid_v; substr(\$grid_v, 18, 1, '5'
+); }) |
(?(?{ !valid(0, 2, '6') })x)(?{ local \$grid_h = \$grid_h; substr(\$gr
+id_h, 2, 1, '6'); local \$grid_v = \$grid_v; substr(\$grid_v, 18, 1, '6'
+); }) |
(?(?{ !valid(0, 2, '7') })x)(?{ local \$grid_h = \$grid_h; substr(\$gr
+id_h, 2, 1, '7'); local \$grid_v = \$grid_v; substr(\$grid_v, 18, 1, '7'
+); }) |
(?(?{ !valid(0, 2, '8') })x)(?{ local \$grid_h = \$grid_h; substr(\$gr
+id_h, 2, 1, '8'); local \$grid_v = \$grid_v; substr(\$grid_v, 18, 1, '8'
+); }) |
(?(?{ !valid(0, 2, '9') })x)(?{ local \$grid_h = \$grid_h; substr(\$gr
+id_h, 2, 1, '9'); local \$grid_v = \$grid_v; substr(\$grid_v, 18, 1, '9'
+); })
)
.
.
.
(?:
(?(?{ !valid(8, 6, '1') })x)(?{ local \$grid_h = \$grid_h; substr(\$gr
+id_h, 78, 1, '1'); local \$grid_v = \$grid_v; substr(\$grid_v, 62, 1, '1
+'); }) |
(?(?{ !valid(8, 6, '2') })x)(?{ local \$grid_h = \$grid_h; substr(\$gr
+id_h, 78, 1, '2'); local \$grid_v = \$grid_v; substr(\$grid_v, 62, 1, '2
+'); }) |
(?(?{ !valid(8, 6, '3') })x)(?{ local \$grid_h = \$grid_h; substr(\$gr
+id_h, 78, 1, '3'); local \$grid_v = \$grid_v; substr(\$grid_v, 62, 1, '3
+'); }) |
(?(?{ !valid(8, 6, '4') })x)(?{ local \$grid_h = \$grid_h; substr(\$gr
+id_h, 78, 1, '4'); local \$grid_v = \$grid_v; substr(\$grid_v, 62, 1, '4
+'); }) |
(?(?{ !valid(8, 6, '5') })x)(?{ local \$grid_h = \$grid_h; substr(\$gr
+id_h, 78, 1, '5'); local \$grid_v = \$grid_v; substr(\$grid_v, 62, 1, '5
+'); }) |
(?(?{ !valid(8, 6, '6') })x)(?{ local \$grid_h = \$grid_h; substr(\$gr
+id_h, 78, 1, '6'); local \$grid_v = \$grid_v; substr(\$grid_v, 62, 1, '6
+'); }) |
(?(?{ !valid(8, 6, '7') })x)(?{ local \$grid_h = \$grid_h; substr(\$gr
+id_h, 78, 1, '7'); local \$grid_v = \$grid_v; substr(\$grid_v, 62, 1, '7
+'); }) |
(?(?{ !valid(8, 6, '8') })x)(?{ local \$grid_h = \$grid_h; substr(\$gr
+id_h, 78, 1, '8'); local \$grid_v = \$grid_v; substr(\$grid_v, 62, 1, '8
+'); }) |
(?(?{ !valid(8, 6, '9') })x)(?{ local \$grid_h = \$grid_h; substr(\$gr
+id_h, 78, 1, '9'); local \$grid_v = \$grid_v; substr(\$grid_v, 62, 1, '9
+'); })
)
(?:
(?(?{ !valid(8, 7, '1') })x)(?{ local \$grid_h = \$grid_h; substr(\$gr
+id_h, 79, 1, '1'); local \$grid_v = \$grid_v; substr(\$grid_v, 71, 1, '1
+'); }) |
(?(?{ !valid(8, 7, '2') })x)(?{ local \$grid_h = \$grid_h; substr(\$gr
+id_h, 79, 1, '2'); local \$grid_v = \$grid_v; substr(\$grid_v, 71, 1, '2
+'); }) |
(?(?{ !valid(8, 7, '3') })x)(?{ local \$grid_h = \$grid_h; substr(\$gr
+id_h, 79, 1, '3'); local \$grid_v = \$grid_v; substr(\$grid_v, 71, 1, '3
+'); }) |
(?(?{ !valid(8, 7, '4') })x)(?{ local \$grid_h = \$grid_h; substr(\$gr
+id_h, 79, 1, '4'); local \$grid_v = \$grid_v; substr(\$grid_v, 71, 1, '4
+'); }) |
(?(?{ !valid(8, 7, '5') })x)(?{ local \$grid_h = \$grid_h; substr(\$gr
+id_h, 79, 1, '5'); local \$grid_v = \$grid_v; substr(\$grid_v, 71, 1, '5
+'); }) |
(?(?{ !valid(8, 7, '6') })x)(?{ local \$grid_h = \$grid_h; substr(\$gr
+id_h, 79, 1, '6'); local \$grid_v = \$grid_v; substr(\$grid_v, 71, 1, '6
+'); }) |
(?(?{ !valid(8, 7, '7') })x)(?{ local \$grid_h = \$grid_h; substr(\$gr
+id_h, 79, 1, '7'); local \$grid_v = \$grid_v; substr(\$grid_v, 71, 1, '7
+'); }) |
(?(?{ !valid(8, 7, '8') })x)(?{ local \$grid_h = \$grid_h; substr(\$gr
+id_h, 79, 1, '8'); local \$grid_v = \$grid_v; substr(\$grid_v, 71, 1, '8
+'); }) |
(?(?{ !valid(8, 7, '9') })x)(?{ local \$grid_h = \$grid_h; substr(\$gr
+id_h, 79, 1, '9'); local \$grid_v = \$grid_v; substr(\$grid_v, 71, 1, '9
+'); })
)
(?:
(?(?{ !valid(8, 8, '1') })x)(?{ local \$grid_h = \$grid_h; substr(\$gr
+id_h, 80, 1, '1'); local \$grid_v = \$grid_v; substr(\$grid_v, 80, 1, '1
+'); }) |
(?(?{ !valid(8, 8, '2') })x)(?{ local \$grid_h = \$grid_h; substr(\$gr
+id_h, 80, 1, '2'); local \$grid_v = \$grid_v; substr(\$grid_v, 80, 1, '2
+'); }) |
(?(?{ !valid(8, 8, '3') })x)(?{ local \$grid_h = \$grid_h; substr(\$gr
+id_h, 80, 1, '3'); local \$grid_v = \$grid_v; substr(\$grid_v, 80, 1, '3
+'); }) |
(?(?{ !valid(8, 8, '4') })x)(?{ local \$grid_h = \$grid_h; substr(\$gr
+id_h, 80, 1, '4'); local \$grid_v = \$grid_v; substr(\$grid_v, 80, 1, '4
+'); }) |
(?(?{ !valid(8, 8, '5') })x)(?{ local \$grid_h = \$grid_h; substr(\$gr
+id_h, 80, 1, '5'); local \$grid_v = \$grid_v; substr(\$grid_v, 80, 1, '5
+'); }) |
(?(?{ !valid(8, 8, '6') })x)(?{ local \$grid_h = \$grid_h; substr(\$gr
+id_h, 80, 1, '6'); local \$grid_v = \$grid_v; substr(\$grid_v, 80, 1, '6
+'); }) |
(?(?{ !valid(8, 8, '7') })x)(?{ local \$grid_h = \$grid_h; substr(\$gr
+id_h, 80, 1, '7'); local \$grid_v = \$grid_v; substr(\$grid_v, 80, 1, '7
+'); }) |
(?(?{ !valid(8, 8, '8') })x)(?{ local \$grid_h = \$grid_h; substr(\$gr
+id_h, 80, 1, '8'); local \$grid_v = \$grid_v; substr(\$grid_v, 80, 1, '8
+'); }) |
(?(?{ !valid(8, 8, '9') })x)(?{ local \$grid_h = \$grid_h; substr(\$gr
+id_h, 80, 1, '9'); local \$grid_v = \$grid_v; substr(\$grid_v, 80, 1, '9
+'); })
)
(?{ \$match_grid = \$grid_h })
/x;

In reply to Re: The craziest RegExes you ever created by ikegami
in thread The craziest RegExes you ever created by Ieronim

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

• Are you posting in the right place? Check out Where do I post X? to know for sure.
• Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
• Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
• Want more info? How to link or or How to display code and escape characters are good places to start.

Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2022-08-17 19:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?

No recent polls found

Notices?