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.
如果我使用 aRegEx作为 a 的掩码,TextBox并且掩码应该允许 的格式000-XXXXXX,例如,它允许 3 个字母、一个破折号,然后是 6 个数字,我怎么能让用户只需要输入前 3 个在搜索中使用的掩码字符并且没有他们输入的内容是无效的,因为它不满足完整的RegEx?
RegEx
TextBox
000-XXXXXX
您可以使正则表达式的某些部分成为可选的:
^\d{3}(?:-\d{0,6})?$
解释:
^ # Start of string \d{3} # Match 3 digits (?: # Try to match... - # a dash \d{0,6} # followed by up to 6 digits )? # but make that part of the match optional $ # End of string