You may try:
^(?![- ])(?:(?:97[89])?([- ]?)(?=(?:d1?){9}1[dxX]$)(?:d+1){3}[dxX]|(?=.{11}([- ])[dxX]$)(?:d+2){3}[dxX])
See the online demo
^
- Start string anchor.
(?![- ])
- Negative lookahead to prevent a leading hyphen or space.
(?:
- Open non-capture group:
(?:
- Open non-capture group:
97[89]
- Match "97" followed by either an 8 or 9.
)?
- Close non-capture group and make it optional.
(
- Open 1st capture group:
[- ]?
- Optionally match an hyphen or space.
)
- Close 1st capture group.
(?=
- Open positive lookahead:
(?:
- Open non-capture group:
d1?
- Capture a digit and optionally match what is captured in the 1st capture group.
){9}
- Close the non-capture group and match it nine times (to assert a position with 9 digits ahead).
1[dxX]$
- Again match what is captured in the 1st capture group followed by a digit or lower- or uppercase "x" and the end string anchor.
)
- Close positive lookahead.
(?:
- Open non-capture group:
d+1
- 1+ digits followed by what is captured in the 1st capture group.
){3}
- Close the non-capture group and match it three times.
[dxX]
- Match a digit, a lower- or uppercase "x".
|
- Or:
(?=
- Open positive lookahead:
.{11}
- Match eleven characters other than newline.
([- ])
- OA 2nd capture group to match either hypen or space.
[dxX]$
- Match a digit, a lower- or uppercase "x" up to end string anchor.
)
- Close positive lookahead.
(?:
- Open non-capture group:
d+2
- Match 1+ digits and what is captured in the 2nd capture group.
){3}
- Close non-capture group and match three times.
[dxX]$
- Match a digit, a lower- or uppercase "x".
)
- Close non-capture group.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…