[英]regular expression: The beginning and end of the string with a letter with a specified length
我使用這種模式,但我沒有得到答案。 Regex reg = new Regex(@"^([A-Za-z][A-Za-z0-9\.]*(?:[A-Za-z])){6,30}@mydomain.com$");
我希望我的字符串以字母開頭並以字母結尾,由字母、數字和點組成,只要它的長度在 6 到 30 個字符之間。
例如:a.124b@mydomain.com 或 abc.1e@mydomain.com 和...
string pattern = @"^[a-z][a-z0-9.]{4,28}[a-z]@mydomain\.com$";
string input = @"a.124b@mydomain.com";
RegexOptions options = RegexOptions.IgnoreCase;
foreach (Match m in Regex.Matches(input, pattern, options))
{
Console.WriteLine("'{0}' found at index {1}.", m.Value, m.Index);
}
現在解釋:
^[az]
字符串的開頭和一個字母[a-z0-9.]{4,28}
字母、數字和點字符(在方括號中時不需要轉義),重復 4 到 28 次[az]
另一個單個字母@mydomain\.com$
rest 您的郵件地址和字符串結尾。 還要注意RegexOptions.IgnoreCase
- 當您知道自己不關心大小寫時,它會使字母組更具可讀性
您在正則表達式中犯的錯誤是為您的完整捕獲組添加量詞 - 意味着整個組重復 6-30 次。
我還推薦https://regex101.com/來滿足您的所有正則表達式需求
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.