亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

用于將電子郵件地址與域匹配的正則表達式

用于將電子郵件地址與域匹配的正則表達式

C#
鴻蒙傳說 2021-12-25 16:46:34
鑒于我有域模式domain.comsub.domain.com*.com*.domain.comsub.*.com鑒于我將有以下格式的電子郵件地址[email protected]@sub.domain.com是否可以制作 1 個正則表達式模式來查找電子郵件地址是否與域模式之一匹配?
查看完整描述

3 回答

?
狐的傳說

TA貢獻1804條經驗 獲得超3個贊

在這種情況下,*.com 包括所有其他模式。您可以檢查電子郵件endwith .com是否足以滿足您的需求


List<string> emails = new List<string>

                      {

                          "[email protected]",

                          "[email protected]",

                          "[email protected]",

                          "[email protected]",

                          "[email protected]",

                      };


Regex regex = new Regex(@"(domain\.com)|(sub\.*\.com)");


emails= emails.Where(e => regex.IsMatch(e)).ToList();

如果要使用正則表達式檢查多個模式,可以使用 |


List<string> emails = new List<string>

                      {

                          "[email protected]",

                          "[email protected]",

                          "[email protected]",

                          "[email protected]",

                          "[email protected]",

                      };


Regex regex = new Regex(@"(domain.com)|(sub.*.com)");


emails= emails.Where(e => regex.IsMatch(e)).ToList();


查看完整回答
反對 回復 2021-12-25
?
縹緲止盈

TA貢獻2041條經驗 獲得超4個贊

您不需要正則表達式??梢杂?linq 完成:


            string[] domains = { "*.sub.domain.com", "*.com", "*.domain.com", "sub.*.com" };


            string email = "abc.sub.domain.com";

            string user = "abc";


            bool match = domains.Any(x => x.Replace("*", user) == email);


查看完整回答
反對 回復 2021-12-25
?
慕萊塢森

TA貢獻1810條經驗 獲得超4個贊

試試這個正則表達式:

^ [a-zA-Z0-9 _. + -] + @ (?: (?: [a-zA-Z0-9 -] +.)? [a-zA-Z] +.)? (provider1 | provider2 ). (domain1 | domain2) $

用這個例子嘗試了這個正則表達式:[email protected] - 是有效的

帶有子域的電子郵件的正則表達式:

^ [a-zA-Z0-9 _. + -] + @ (?: (?: [a-zA-Z0-9 -] +.)? [a-zA-Z] +.)? (sub1 | sub2 ). (provider1 | provider2). (domain1 | domain2) $

它適用于任何域和郵件服務提供商。我試過這個,它有效


查看完整回答
反對 回復 2021-12-25
  • 3 回答
  • 0 關注
  • 214 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號