ゼロ幅先読みアサーションを使用して桁数を制限すると、長さの制限を気にせずにパターンの残りの部分を書き込むことができます。これを試して:
&#(?=x?[0-9A-Fa-f]{1,4})0*([12]?\d|3[01]|x0*1?[0-9A-Fa-f]);
説明:
(?=x?[0-9A-Fa-f]{1,4}) #Restricts the numeric portion to at most four digits, including leading zeroes.
0* #Consumes leading zeroes if there is no x.
[12]?\d #Allows decimal numbers 0 - 29, inclusive.
3[01] #Allows decimal 30 or 31.
x0*1?[0-9A-Fa-f] #Allows hexadecimal 0 - 1F, inclusive, regardless of case or leading zeroes.
このパターンでは、の後に先行ゼロを使用できますx
が、パーツによって、の前にゼロが(?=x?[0-9A-Fa-f]{1,4})
発生するのを防ぎます。x