Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在寻找一个正则表达式来匹配 9 个数字字符和可选的 1 或 2 个连字符。连字符的位置无关紧要。我能够让正则表达式有一个连字符:
^([0-9]{9}|(?=^[^-]+-[^-]+$)[0-9-]{10})$
如何修改它以包含另一个连字符?
以下使用前瞻并确保它符合规则,即它由带有 0-2 个连字符的数字组成。
(?=^\d*-?\d*-?\d*$)^-*(\d-*){9}$
这应该这样做
^(?!([^-]*-){3,})(?=(\D*\d\D*){9}$)[\d-]+$ --------------- ---------------- | |->match only if there are 9 digits | |->don't match if there are 3 or more -