數據匹配之正則表達式

先來了解一下各字符代表的意思:
\d :digit(0-9) 數字0-9
\D :not a digit 非數字
\w :word character(a-z,A-Z,0-9,_) 大小寫字母數字下劃線
\W :not a word character 大小寫字母數字下劃線以外
\s :whitespace(space,tab,blank,line) 空格、tab。。
\S :non_whitespace 非空格

\b :word boundary
\B :not a word boundary
\A :start of string 表示開頭,也可以用^
\Z :end of string 表示結尾,也可以用$
\g

\ :escape special character 轉意符,加上\之後表示該字符本身
. :Matches any characters 匹配任意字符
$ :matches end of string 匹配結尾
^ :matches beginning of string 匹配開頭
[] :matches characters in brackets 匹配[]裏的任意一個字符
[^] :matches characters not in brackets 匹配[]以外的字符
| :Either or 或
() :group 分組

“*” :0 or more 匹配0次或多次
“+” :1 or more 匹配1次以上
? :0 or 1 匹配0次或1次
{m} :exactly m times 匹配m次
{n,} :min n times 匹配至少n次
{m,n} :from m to n times 匹配m次到n次
{m,n}?:from m to n,as few as possible 匹配m次到n次之間儘量少的次數

下面是練習部分:
1、匹配以1開頭的電話號碼:1xx-xxxx-xxxx
1\d{2}(-\d{4}){2}
2、匹配郵箱:
1[\w.-]+@[a-zA-Z0-9][\w.]{2,}


  1. a-zA-Z0-9 ↩︎

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章