至少一个数字,至少8个字符长度,unicodeAt least one digit, minimum 8 chars length, with unicode

- 此内容更新于:2015-01-06
主题:

我知道正则表达式的问题已经问很多次,但我不能让它,因为我需要工作。我需要的是一个正则表达式,至少8个字符,包含至少一个数字(数字可以出现在一开始,结束后或其他字符),和支持Unicode,希伯来语,阿拉伯语等字符可以使用。

原文:

I know that regex questions have been asked many times before, but I just can't make it to work as I need. What I need is a regex, with a minimum of 8 characters, containing at least one digit (digits can appear in the start, end or after other characters), and supporting Unicode, so that Hebrew, Arabic etc. characters can be used.

JonM的回复:Unicode支持随每个实现正则表达式。你在哪里使用这个表情?

(原文:Unicode support varies with each implementation of regex. Where are you using this expression?)

Evert的回复:这个问题(回答)可以帮助。你可能需要替换10 8日。

(原文:This question (and answer) could help. You would likely need to replace the 10 with 8,.)

ashilon的回复:我# 39;使用ASP.NET。但似乎卢卡斯# 39;和Andie2302& # 39;年代答案伟大的工作。

(原文:I'm using ASP.NET. But it seems that Lucas's and Andie2302's answers work great.)

解决方案:
原文:

Here's the basic regex:

^(?=.*?d).{8}

^.{8} will match any string that has at least 8 characters. (?=.*?d) will assert there's a digit in there.

As for the Unicode support, that's up to the regex engine. If Unicode is supported, . should match a Unicode character. If you want to match graphemes instead, your regex flavor may support X, which you could use instead of ..

If you want to allow non-latin digits, you may need to replace d with p{N} depending on your regex engine.

Update for the .NET flavor:

  • d already matches Unicode digits so you don't need to use p{N}
  • X is not supported so you'll have to stick with . or use a workaround like (?>P{M}p{M}*).
ashilon的回复:非常感谢卢卡斯,似乎很好地工作。

(原文:Thank you very much Locas, that seems to work just fine.)

ashilon的回复:看样子我忘了提,如果字符串只包含数字,匹配失败,所以88888893应该失败。

(原文:Well, it seems that I forgot to mention that if the string contains only digits, the match fails, so 88888893 should fail.)

Lucas Trzesniewski的回复:@ashilon之后^可以添加(? = . * ? D)或(? = . * ? p { L })。 D需要non-digit p { L }需要一封信。完整的正则表达式将类似^(? = . * d)(? = . * d)。{ 8 }

(原文:@ashilon Then after the ^ you can add (?=.*?D) or (?=.*?p{L}). D will require a non-digit, and p{L} will require a letter. The full regex will be something like ^(?=.*?d)(?=.*?D).{8})

解决方案:
假设您使用的是c#或者Java正则表达式的味道,和你的意思 与字符的unicode字符类别“字母” 使用: (? = p { L } * ? p {和})[ p { L } p {和}]{ 8 }
原文:

Assuming you are using a C# or Java like regex flavor, and you mean with characters a character of the unicode category "letter" you can use:

(?=p{L}*?p{Nd})[p{L}p{Nd}]{8,}

ashilon的回复:谢谢Andi2302,您的解决方案。看来,用正则表达式,总是会有许多办法来解决这个问题。再次感谢

(原文:Thank you Andi2302, your solution works too. Seems that with regular expressions, there always are many ways to solve the problem. Thanks again)