⑴ 從鍵盤中輸入一個Email,定義正則表達式並驗證輸入的Email是否正確。
var reg = /^\w+((-\w+)|(\.\w+))*\@[a-za-z0-9]+((\.|-)[a-za-z0-9]+)*\.[a-za-z0-9]+$/;
if (!reg.test(email)) { alert("請您輸入正確的郵箱!"); return false; }上邊的是郵箱驗證,正則格式:字母或數字+@+字母或數字+.+字母或數字
望採納!
⑵ 正則表達式如何驗證郵箱
1.PHP郵箱驗證正則表達式:x0dx0apreg_match("/^[0-9a-zA-Z]+@(([0-9a-zA-Z]+)[.])+[a-z]{2,4}$/i",$email);x0dx0a如果需要更加完善、嚴格的驗證,修改這個正則表達式即可。x0dx0ax0dx0a2.PHP郵箱驗證正則表達式實例:x0dx0ax0dx0ax0dx0a3.Javascript(js)郵箱驗證正則表達式:x0dx0amyreg=/^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,4}$/;x0dx0a這個可以驗證形如:[email protected],[email protected]這種郵箱x0dx0ax0dx0a4.Javascript(js)郵箱驗證正則表達式實例:x0dx0a
⑶ 怎樣用正則表達式驗證輸入的郵箱,答對且詳細者,我給高分!
你可以在客戶端用JS判斷,代碼如下:
JS代碼:
<script. type="text/javascript">
function checkemail()
{
var temp = document.getElementById("email");
var myreg = /(\S)+[@]{1}(\S)+[.]{1}(\w)+/;
if(temp.value!="")
{
if(!myreg.test(temp.value))
{
document.getElementById("mail").innerHTML="請輸入有效的email!";
document.getElementById("mail").style.color="red";
temp.value="";
temp.focus();
return false;
}
else{
document.getElementById("mail").innerHTML="email可以使用";
document.getElementById("mail").style.color="green";
}
}
}
</script>
html如下:
這里我只寫在body裡面的代碼:
郵箱地址:<input id="email" name="email" nBlur="checkemail()" type="text" /><span id="mail"></span>
⑷ c#中如何利用正則表達式判定郵箱的合法性
[csharp]
using System;
using System.Text.RegularExpressions;
namespace SG_VQCDataCollection
{
/// <summary>
/// 通過Framwork類庫中的Regex類實現了一些特殊功能數據檢查
/// </summary>
public class MetarnetRegex
{
private static MetarnetRegex instance = null;
public static MetarnetRegex GetInstance()
{
if (MetarnetRegex.instance == null)
{
MetarnetRegex.instance = new MetarnetRegex();
}
return MetarnetRegex.instance;
}
private MetarnetRegex()
{
}
/// <summary>
/// 判斷輸入的字元串只包含漢字
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static bool IsChineseCh(string input)
{
//Regex regex = new Regex("^[\一-\龥]+$");
//改了一下
Regex regex = new Regex("^[\一-\龥]+$");
return regex.IsMatch(input);
}
/// <summary>
/// 匹配3位或4位區號的電話號碼,其中區號可以用小括弧括起來,
/// 也可以不用,區號與本地號間可以用連字型大小或空格間隔,
/// 也可以沒有間隔
/// \(0\d{2}\)[- ]?\d{8}|0\d{2}[- ]?\d{8}|\(0\d{3}\)[- ]?\d{7}|0\d{3}[- ]?\d{7}
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static bool IsPhone(string input)
{
string pattern = "^\\(0\\d{2}\\)[- ]?\\d{8}$|^0\\d{2}[- ]?\\d{8}$|^\\(0\\d{3}\\)[- ]?\\d{7}$|^0\\d{3}[- ]?\\d{7}$";
Regex regex = new Regex(pattern);
return regex.IsMatch(input);
}
/// <summary>
/// 判斷輸入的字元串是否是一個合法的手機號
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static bool IsMobilePhone(string input)
{
Regex regex = new Regex("^13\\d{9}$");
return regex.IsMatch(input);
}
/// <summary>
/// 判斷輸入的字元串只包含數字
/// 可以匹配整數和浮點數
/// ^-?\d+$|^(-?\d+)(\.\d+)?$
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static bool IsNumber(string input)
{
string pattern = "^-?\\d+$|^(-?\\d+)(\\.\\d+)?$";
Regex regex = new Regex(pattern);
return regex.IsMatch(input);
}
/// <summary>
/// 匹配非負整數
///
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static bool IsNotNagtive(string input)
{
Regex regex = new Regex(@"^\d+$");
return regex.IsMatch(input);
}
/// <summary>
/// 匹配正整數
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static bool IsUint(string input)
{
Regex regex = new Regex("^[0-9]*[1-9][0-9]*$");
return regex.IsMatch(input);
}
/// <summary>
/// 判斷輸入的字元串字包含英文字母
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static bool IsEnglisCh(string input)
{
Regex regex = new Regex("^[A-Za-z]+$");
return regex.IsMatch(input);
}
/// <summary>
/// 判斷輸入的字元串是否是一個合法的Email地址
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static bool IsEmail(string input)
{
string pattern = @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
Regex regex = new Regex(pattern);
return regex.IsMatch(input);
}
/// <summary>
/// 判斷輸入的字元串是否只包含數字和英文字母
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static bool IsNumAndEnCh(string input)
{
string pattern = @"^[A-Za-z0-9]+$";
Regex regex = new Regex(pattern);
return regex.IsMatch(input);
}
/// <summary>
/// 判斷輸入的字元串是否是一個超鏈接
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static bool IsURL(string input)
{
//string pattern = @"http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?";
string pattern = @"^[a-zA-Z]+://(\w+(-\w+)*)(\.(\w+(-\w+)*))*(\?\S*)?$";
Regex regex = new Regex(pattern);
return regex.IsMatch(input);
}
/// <summary>
/// 判斷輸入的字元串是否是表示一個IP地址
/// </summary>
/// <param name="input">被比較的字元串</param>
/// <returns>是IP地址則為True</returns>
public static bool IsIPv4(string input)
{
string[] IPs = input.Split('.');
Regex regex = new Regex(@"^\d+$");
for (int i = 0; i < IPs.Length; i++)
{
if (!regex.IsMatch(IPs[i]))
{
return false;
}
if (Convert.ToUInt16(IPs[i]) > 255)
{
return false;
}
}
return true;
}
/// <summary>
/// 計算字元串的字元長度,一個漢字字元將被計算為兩個字元
/// </summary>
/// <param name="input">需要計算的字元串</param>
/// <returns>返回字元串的長度</returns>
public static int GetCount(string input)
{
return Regex.Replace(input, @"[\一-\龥/g]", "aa").Length;
}
/// <summary>
/// 調用Regex中IsMatch函數實現一般的正則表達式匹配
/// </summary>
/// <param name="pattern">要匹配的正則表達式模式。</param>
/// <param name="input">要搜索匹配項的字元串</param>
/// <returns>如果正則表達式找到匹配項,則為 true;否則,為 false。</returns>
public static bool IsMatch(string pattern, string input)
{
Regex regex = new Regex(pattern);
return regex.IsMatch(input);
}
/// <summary>
/// 從輸入字元串中的第一個字元開始,用替換字元串替換指定的正則表達式模式的所有匹配項。
/// </summary>
/// <param name="pattern">模式字元串</param>
/// <param name="input">輸入字元串</param>
/// <param name="replacement">用於替換的字元串</param>
/// <returns>返回被替換後的結果</returns>
public static string Replace(string pattern, string input, string replacement)
{
Regex regex = new Regex(pattern);
return regex.Replace(input, replacement);
}
/// <summary>
/// 在由正則表達式模式定義的位置拆分輸入字元串。
/// </summary>
/// <param name="pattern">模式字元串</param>
/// <param name="input">輸入字元串</param>
/// <returns></returns>
public static string[] Split(string pattern, string input)
{
Regex regex = new Regex(pattern);
return regex.Split(input);
}
/// <summary>
/// 判斷輸入的字元串是否是合法的IPV6 地址
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static bool IsIPV6(string input)
{
string pattern = "";
string temp = input;
string[] strs = temp.Split(':');
if (strs.Length > 8)
{
return false;
}
int count = MetarnetRegex.GetStringCount(input, "::");
if (count > 1)
{
return false;
}
else if (count == 0)
{
pattern = @"^([\da-f]{1,4}:){7}[\da-f]{1,4}$";
Regex regex = new Regex(pattern);
return regex.IsMatch(input);
}
else
{
pattern = @"^([\da-f]{1,4}:){0,5}::([\da-f]{1,4}:){0,5}[\da-f]{1,4}$";
Regex regex1 = new Regex(pattern);
return regex1.IsMatch(input);
}
}
/* *******************************************************************
* 1、通過「:」來分割字元串看得到的字元串數組長度是否小於等於8
* 2、判斷輸入的IPV6字元串中是否有「::」。
* 3、如果沒有「::」採用 ^([\da-f]{1,4}:){7}[\da-f]{1,4}$ 來判斷
* 4、如果有「::」 ,判斷"::"是否止出現一次
* 5、如果出現一次以上 返回false
* 6、^([\da-f]{1,4}:){0,5}::([\da-f]{1,4}:){0,5}[\da-f]{1,4}$
* ******************************************************************/
/// <summary>
/// 判斷字元串compare 在 input字元串中出現的次數
/// </summary>
/// <param name="input">源字元串</param>
/// <param name="compare">用於比較的字元串</param>
/// <returns>字元串compare 在 input字元串中出現的次數</returns>
private static int GetStringCount(string input, string compare)
{
int index = input.IndexOf(compare);
if (index != -1)
{
return 1 + GetStringCount(input.Substring(index + compare.Length), compare);
}
else
{
return 0;
}
}
}
}
⑸ 求正則表達式驗證郵箱格式
雖然沒有統一的郵箱賬號格式,但是所有郵箱都符合「名稱@域名」的規律。對於名稱和域名的字元限制,我們可以根據項目的情況定義一個,比如只允許有英文、數字、下劃線等組成。下面舉例實現一些驗證郵箱格式的正則表達式。
實例1:只允許英文字母、數字、下劃線、英文句號、以及中劃線組成
分析郵件名稱部分:
26個大小寫英文字母表示為a-zA-Z
數字表示為0-9
下劃線表示為_
中劃線表示為-
由於名稱是由若干個字母、數字、下劃線和中劃線組成,所以需要用到+表示多次出現,根據以上條件得出郵件名稱表達式:[a-zA-Z0-9_-]+
分析域名部分:一般域名的規律為「[N級域名][三級域名.]二級域名.頂級域名」,比如「qq.com」、「www.qq.com」、「mp.weixin.qq.com」、「12-34.com.cn」,分析可得域名類似「**.**.**.**」組成。
「**」部分可以表示為[a-zA-Z0-9_-]+
「.**」部分可以表示為.[a-zA-Z0-9_-]+
多個「.**」可以表示為(.[a-zA-Z0-9_-]+)+
綜上所述,域名部分可以表示為[a-zA-Z0-9_-]+(.[a-zA-Z0-9_-]+)+
最終表達式: 由於郵箱的基本格式為「名稱@域名」,需要使用「^」匹配郵箱的開始部分,用「$」匹配郵箱結束部分以保證郵箱前後不能有其他字元,所以最終郵箱的正則表達式為:
^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(.[a-zA-Z0-9_-]+)+$
實例2:名稱允許漢字、字母、數字,域名只允許英文域名
舉例:張三[email protected]
分析郵件名稱部分:
漢字在正則表示為[u4e00-u9fa5]
字母和數字表示為A-Za-z0-9
通過分析得出郵件名稱部分表達式為[A-Za-z0-9u4e00-u9fa5]+
分析郵件域名部分:郵件部分可以參考實例1中的分析域名部分。
得出域名部分的表達式為[a-zA-Z0-9_-]+(.[a-zA-Z0-9_-]+)+。
最終表達式: 我們用@符號將郵箱的名稱和域名拼接起來,因此完整的郵箱表達式為 :
^[A-Za-z0-9u4e00-u9fa5]+@[a-zA-Z0-9_-]+(.[a-zA-Z0-9_-]+)+$
⑹ C語言怎麼用正則表達式
由於它可以極大地簡化處理字元串時的復雜度,因此現在已經在許多 L i n u x 實用工具中得到了應用。千萬不要以為正則表達式只是 P e r l 、 P y t h o n 、 B a s h 等腳本語言的專利,作為 C 語言程序員,用戶同樣可以在自己的程序中運用正則表達式。標準的 C 和 C + + 都不支持正則表達式,但有一些函數庫可以輔助 C / C + + 程序員完成這一功能,其中最著名的當數 P h i l i p H a z e l 的 P e r l - C o m p a t i b l e R e g u l a r E x p r e s s i o n 庫,許多 L i n u x 發行版本都帶有這個函數庫。編譯正則表達式為了提高效率,在將一個字元串與正則表達式進行比較之前,首先要用 r e g c o m p ( ) 函數對它進行編譯,將其轉化為 r e g e x _ t 結構: i n t r e g c o m p ( r e g e x _ t * p r e g , c o n s t c h a r * r e g e x , i n t c f l a g s ) ; 參數 r e g e x 是一個字元串,它代表將要被編譯的正則表達式;參數 p r e g 指向一個聲明為 r e g e x _ t 的數據結構,用來保存編譯結果;參數 c f l a g s 決定了正則表達式該如何被處理的細節。如果函數 r e g c o m p ( ) 執行成功,並且編譯結果被正確填充到 p r e g 中後,函數將返回 0 ,任何其它的返回結果都代表有某種錯誤產生。匹配正則表達式一旦用 r e g c o m p ( ) 函數成功地編譯了正則表達式,接下來就可以調用 r e g e x e c ( ) 函數完成模式匹配: i n t r e g e x e c ( c o n s t r e g e x _ t * p r e g , c o n s t c h a r * s t r i n g , s i z e _ t n m a t c h , r e g m a t c h _ t p m a t c h [ ] , i n t e f l a g s ) ; t y p e d e f s t r u c t { r e g o f f _ t r m _ s o ; r e g o f f _ t r m _ e o ; } r e g m a t c h _ t ; 參數 p r e g 指向編譯後的正則表達式,參數 s t r i n g 是將要進行匹配的字元串,而參數 n m a t c h 和 p m a t c h 則用於把匹配結果返回給調用程序,最後一個參數 e f l a g s 決定了匹配的細節。在調用函數 r e g e x e c ( ) 進行模式匹配的過程中,可能在字元串 s t r i n g 中會有多處與給定的正則表達式相匹配,參數 p m a t c h 就是用來保存這些匹配位置的,而參數 n m a t c h 則告訴函數 r e g e x e c ( ) 最多可以把多少個匹配結果填充到 p m a t c h 數組中。當 r e g e x e c ( ) 函數成功返回時,從 s t r i n g + p m a t c h [ 0 ] . r m _ s o 到 s t r i n g + p m a t c h [ 0 ] . r m _ e o 是第一個匹配的字元串,而從 s t r i n g + p m a t c h [ 1 ] . r m _ s o 到 s t r i n g + p m a t c h [ 1 ] . r m _ e o ,則是第二個匹配的字元串,依此類推。釋放正則表達式無論什麼時候,當不再需要已經編譯過的正則表達式時,都應該調用函數 r e g f r e e ( ) 將其釋放,以免產生內存泄漏。 v o i d r e g f r e e ( r e g e x _ t * p r e g ) ; 函數 r e g f r e e ( ) 不會返回任何結果,它僅接收一個指向 r e g e x _ t 數據類型的指針,這是之前調用 r e g c o m p ( ) 函數所得到的編譯結果。如果在程序中針對同一個 r e g e x _ t 結構調用了多次 r e g c o m p ( ) 函數, P O S I X 標准並沒有規定是否每次都必須調用 r e g f r e e ( ) 函數進行釋放,但建議每次調用 r e g c o m p ( ) 函數對正則表達式進行編譯後都調用一次 r e g f r e e ( ) 函數,以盡早釋放佔用的存儲空間。報告錯誤信息如果調用函數 r e g c o m p ( ) 或 r e g e x e c ( ) 得到的是一個非 0 的返回值,則表明在對正則表達式的處理過程中出現了某種錯誤,此時可以通過調用函數 r e g e r r o r ( ) 得到詳細的錯誤信息。 s i z e _ t r e g e r r o r ( i n t e r r c o d e , c o n s t r e g e x _ t * p r e g , c h a r * e r r b u f , s i z e _ t e r r b u f _ s i z e ) ; 參數 e r r c o d e 是來自函數 r e g c o m p ( ) 或 r e g e x e c ( ) 的錯誤代碼,而參數 p r e g 則是由函數 r e g c o m p ( ) 得到的編譯結果,其目的是把格式化消息所必須的上下文提供給 r e g e r r o r ( ) 函數。在執行函數 r e g e r r o r ( ) 時,將按照參數 e r r b u f _ s i z e 指明的最大位元組數,在 e r r b u f 緩沖區中填入格式化後的錯誤信息,同時返回錯誤信息的長度。應用正則表達式最後給出一個具體的實例,介紹如何在 C 語言程序中處理正則表達式。 # i n c l u d e < s t d i o . h > ; # i n c l u d e < s y s / t y p e s . h > ; # i n c l u d e < r e g e x . h > ; / * 取子串的函數 * / s t a t i c c h a r * s u b s t r ( c o n s t c h a r * s t r , u n s i g n e d s t a r t , u n s i g n e d e n d ) { u n s i g n e d n = e n d - s t a r t ; s t a t i c c h a r s t b u f [ 2 5 6 ] ; s t r n c p y ( s t b u f , s t r + s t a r t , n ) ; s t b u f [ n ] = 0 ; r e t u r n s t b u f ; } / * 主程序 * / i n t m a i n ( i n t a r g c , c h a r * * a r g v ) { c h a r * p a t t e r n ; i n t x , z , l n o = 0 , c f l a g s = 0 ; c h a r e b u f [ 1 2 8 ] , l b u f [ 2 5 6 ] ; r e g e x _ t r e g ; r e g m a t c h _ t p m [ 1 0 ] ; c o n s t s i z e _ t n m a t c h = 1 0 ; / * 編譯正則表達式 * / p a t t e r n = a r g v [ 1 ] ; z = r e g c o m p ( & r e g , p a t t e r n , c f l a g s ) ; i f ( z ! = 0 ) { r e g e r r o r ( z , & r e g , e b u f , s i z e o f ( e b u f ) ) ; f p r i n t f ( s t d e r r , " % s : p a t t e r n ' % s ' \ n " , e b u f , p a t t e r n ) ; r e t u r n 1 ; } / * 逐行處理輸入的數據 * / w h i l e ( f g e t s ( l b u f , s i z e o f ( l b u f ) , s t d i n ) ) { + + l n o ; i f ( ( z = s t r l e n ( l b u f ) ) > ; 0 & & l b u f [ z - 1 ] = = ' \ n ' ) l b u f [ z - 1 ] = 0 ; / * 對每一行應用正則表達式進行匹配 * / z = r e g e x e c ( & r e g , l b u f , n m a t c h , p m , 0 ) ; i f ( z = = R E G _ N O M A T C H ) c o n t i n u e ; e l s e i f ( z ! = 0 ) { r e g e r r o r ( z , & r e g , e b u f , s i z e o f ( e b u f ) ) ; f p r i n t f ( s t d e r r , " % s : r e g c o m ( ' % s ' ) \ n " , e b u f , l b u f ) ; r e t u r n 2 ; } / * 輸出處理結果 * / f o r ( x = 0 ; x < n m a t c h & & p m [ x ] . r m _ s o ! = - 1 ; + + x ) { i f ( ! x ) p r i n t f ( " % 0 4 d : % s \ n " , l n o , l b u f ) ; p r i n t f ( " $ % d = ' % s ' \ n " , x , s u b s t r ( l b u f , p m [ x ] . r m _ s o , p m [ x ] . r m _ e o ) ) ; } } / * 釋放正則表達式 * / r e g f r e e ( & r e g ) ; r e t u r n 0 ; } 上述程序負責從命令行獲取正則表達式,然後將其運用於從標准輸入得到的每行數據,並列印出匹配結果。執行下面的命令可以編譯並執行該程序: # g c c r e g e x p . c - o r e g e x p # . / r e g e x p ' r e g e x [ a - z ] * ' < r e g e x p . c 0 0 0 3 : # i n c l u d e < r e g e x . h > ; $ 0 = ' r e g e x ' 0 0 2 7 : r e g e x _ t r e g ; $ 0 = ' r e g e x ' 0 0 5 4 : z = r e g e x e c ( & r e g , l b u f , n m a t c h , p m , 0 ) ; $ 0 = ' r e g e x e c ' 小結對那些需要進行復雜數據處理的程序來說,正則表達式無疑是一個非常有用的工具。本文重點在於闡述如何在 C 語言中利用正則表達式來簡化字元串處理,以便在數據處理方面能夠獲得與 P e r l 語言類似的靈活性。
⑺ C# 利用正則表達式對輸入的郵箱格式進行匹配
^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$
^匹配字元串的開頭
$匹配字元串的結尾
\w+匹配1到N個數字字母,包括:A-Za-z0-9
真的天書一樣!
看上去似乎還有點問題。
⑻ 正則實現郵箱驗證
當當當!敲黑板(๑•ี_เ•ี๑)
問:什麼是正則?
答:是計算機科學的一種概念,用於檢索匹配或替換符合規則的字元。
今個來寫一個用正則實現的郵箱驗證
首先寫兩個input
1)作為文本輸入
2)作為點擊按鈕並添加文字
2.分別獲取id 給按鈕添加點擊事件 通過if進行判斷
最終效果圖
1)正確
⑼ 驗證電子郵箱的正則表達式
(\\w+)(\\.\\w+)*(\\@\\w+){1}(\\.[a-z]{2,3}){1,2}
簡單的支持 :[email protected](例如:[email protected])
復雜的支持:[email protected]>(.fff.ggg->例如.com.cn)
(例如:[email protected])
\\w :匹配任何字類字元,包括下劃線。與"[A-Za-z0-9_]"等效。
(\\.\\w+)*: 表示".aaa"可以多個也可以沒有.
(\\@\\w+){1}: 表示"@bbb"有且存在一個.
[a-z]{2,3} :表示不需要數字2-3個 例如"aa","bbb".
(\\.[a-z]{2,3}){1,2} :表示 ".aa(a)"有1-2個,例如".com.cn"
⑽ C語言驗證郵箱
演算法並不是很難~
bool fun(char *addr,int len){
int atc=0,ptc=0;
for(int i=0;i<n;i++){
if(str[i]=='@'){
if(atc==1){return false}
atc++;
}else if(str[i]=='.'){
if(atc==0)return false;
if(atc==1)ptc++;
if(ptc==2)return false;
}
}
return true;
}