http://qs321.pair.com?node_id=11109126


in reply to Re^2: implementing a scrabble-esque game on Termux III
in thread implementing a scrabble-esque game on Termux III

$board =~ /(?<!\w).{2,}(?!\w)(?{ push @pat, [ $-[0], $& ] })(*FAIL)/ +;

This is the heart of finding a place to move. For a word to be a legal move, it must match one of the patterns in @pat. The (?<!\w) prevents putting the new word next to a preceding word, and the (?!\w) prevent putting the new word abutting a following word. The(*FAIL) forces the regex engine to try every possible place to find a match.

Try

perl -le ' "abcd" =~ /.{2,}(?{print $&})(*FAIL)/ '

and notice it prints out every substring with two or more characters.