简体   繁体   English

连字符的正则表达式

[英]Regex words with hyphen

I am looking for a regex to match words (no spaces in them) that have at least one number, one letter (a-zA-Z), and at least one hyphen. 我正在寻找一个正则表达式来匹配至少有一个数字,一个字母(a-zA-Z)和至少一个连字符的单词(其中没有空格)。 And the total size should be between 4 and 40. 总大小应在4到40之间。

Examples: 例子:

hi how are you h9-11c matches h9-11c 你好,怎么样h9-11c 匹配 h9-11c

foo 79d-11c-afac bar matches 79d-11c-afac foo 79d-11c-afac bar 匹配 79d-11c-afac

foo dc-afac bar does not match - no number foo dc-afac bar 不匹配 - 没有数字

I have come up with this so far: 到目前为止我想出了这个:

\b(?=.*\d)(?=.*[a-zA-Z])(?=.*-).{4,40}\b

but it doesn't match just one word (it's a bit greedy) 但它只匹配一个词(它有点贪心)

Use \\S to match non-whitespace characters. 使用\\S匹配非空白字符。

Also, \\b matches on boundaries of alphanumberic characters, but you have defined your words to be any non-whitespace. 此外, \\b匹配字母字符的边界,但您已将您的单词定义为任何非空格。

(?<!\S)(?=\S*\d)(?=\S*[a-zA-Z])(?=\S*-)\S{4,40}(?!\S)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM