in reply to Regex logic

So, when you say X and then no X's until the first H, you actually mean X and then no X's and no H until the first H?

X[^XH]*?H[^H]*?D

You didn't supply a program that I can use to test your cases, so I wrote one myself:

perl -lne 'print $1 if /(X[^XH]*?H[^H]*?D)/'

... and for the three test cases, it seems to work.

(Perl) Regular expressions don't have an implicit notion of "first element". If one element does not match, they will try alternation until they find a combination that matches.