[英]checking for alphanumeric and special characters in a string
我的項目中的要求是檢查字符串是否滿足以下條件:
是否有可以滿足所有這些條件的正則表達式?
這是我為此使用的代碼
private bool IsValidFormat(string str)
{
Regex rgx = new Regex(@"^[A-Za-z]+\d+.*$");
return rgx.IsMatch(str);
}
它適用於上面的第1點和第2點,但允許使用特殊字符。 任何幫助,將不勝感激。
以下更改允許至少一個字母,至少一位數字和其他字符。 字母和數字的順序並不重要,這與OP中提供的解決方案不同,該解決方案要求以字母開頭和以數字結尾。
private bool IsValidFormat(string str)
{
Regex rgx = new Regex(@"^(?=.*[0-9])(?=.*[a-zA-Z])([a-zA-Z0-9]+)$");
return rgx.IsMatch(str);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.