從實例學習正則表達式

須要先搭個環境,我用vc+pcre庫html

從網上下載了pcre-8.36.zip和cmake-3.1.3-win32-x86.exe正則表達式

用cmake生成vc的工程文件後,用vc打開工程,編譯生成pcre.dll,pcrecpp.dl等文件,其中pcretest.exe用來測試正則表達式的。工具


在網上搜索驗證郵箱的範例,發現這個寫的比較好 http://www.chinasb.org/archives/2012/02/4270.shtml 學習

pattern應該是   ^[_A-Za-z0-9-]+(\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\.[A-Za-z0-9]+)*(\.[A-Za-z]{2,})$測試


在測試工具中運行以下google


  re> /^[_A-Za-z0-9-]+(\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\.[A-Za-z0-9]+)*(\.[A-Za-z]{2,})$/spa

data> zhiqiang_z.net

No matchhtm

data> zhiqiang_z.info@abc.google.uk教程

 0: zhiqiang_z.info@abc.google.uk

 1: .info

 2: .google

 3: .uk


no match 那個不用說了

match那個,從結果看 0 表明徹底匹配的值

1 表明分組0的值,()裏面就是分組

2 表明分組1的值,

3 表明分組1的值,

若是沒有此分組,則顯示unset

好比

data> zhiqiang_z@164.com

 0: zhiqiang_z@164.com

 1: <unset>

 2: <unset>

 3: .com



須要感謝 《正則表達式30分鐘入門教程》的做者

http://www.oschina.net/question/12_9507


引導個人學習。

相關文章
相關標籤/搜索