python正則表達式學習筆記


import re
re.match #從開始位置開始匹配,如果開頭沒有則無

re.search #搜索整個字符串

re.findall #搜索整個字符串,返回一個list

舉例:

r(raw)用在pattern之前,表示單引號中的字符串爲原生字符,不會進行任何轉義
re.match(r'l','liuyan1').group()  #返回l
re.match(r'y','liuyan1')  #返回None

re.search(r'y','liuyan1').group() #返回y

正則表達式可以包含一些可選標誌修飾符來控制匹配的模式。修飾符被指定爲一個可選的標誌。多個標誌可以通過按位 OR(|) 它們來指定。如 re.I | re.M 被設置成 I 和 M 標誌:

修飾符描述
re.I使匹配對大小寫不敏感
re.L做本地化識別(locale-aware)匹配
re.M多行匹配,影響 ^ 和 $
re.S使 . 匹配包括換行在內的所有字符
re.U根據Unicode字符集解析字符。這個標誌影響 \w, \W, \b, \B.
re.X該標誌通過給予你更靈活的格式以便你將正則表達式寫得更易於理解。

 

re.search(r'[a-z]+','liuyaN1234ab9').group() #返回'liuya'
re.search(r'[a-z]+','liuyaN1234ab9', re.I).group() #返回'liuyaN',對大小寫不敏感
#如果匹配成功,則打印m,否則返回Null
if re.match(r'[0-9]','a'):print 'm'
#用空格分割
 re.split(r'\s+', 'a b   c')
返回:['a', 'b', 'c', 'd']
#用逗號分隔
re.split(r'[\s\,]+', 'a,b, c  d')
返回:['a', 'b', 'c', 'd']

rr=re.match(r'[0-9]','3')

rr.group(0)

 

 

正則表達式模式--http://www.runoob.com/python/python-reg-expressions.html

模式字符串使用特殊的語法來表示一個正則表達式:

字母和數字表示他們自身。一個正則表達式模式中的字母和數字匹配同樣的字符串。

多數字母和數字前加一個反斜槓時會擁有不同的含義。

標點符號只有被轉義時才匹配自身,否則它們表示特殊的含義。

反斜槓本身需要使用反斜槓轉義。

由於正則表達式通常都包含反斜槓,所以你最好使用原始字符串來表示它們。模式元素(如 r'/t',等價於'//t')匹配相應的特殊字符。

下表列出了正則表達式模式語法中的特殊元素。如果你使用模式的同時提供了可選的標誌參數,某些模式元素的含義會改變。

模式描述
^匹配字符串的開頭
$匹配字符串的末尾。
.匹配任意字符,除了換行符,當re.DOTALL標記被指定時,則可以匹配包括換行符的任意字符。
[...]用來表示一組字符,單獨列出:[amk] 匹配 'a','m'或'k'
[^...]不在[]中的字符:[^abc] 匹配除了a,b,c之外的字符。
re*匹配0個或多個的表達式。
re+匹配1個或多個的表達式。
re?匹配0個或1個由前面的正則表達式定義的片段,非貪婪方式
re{ n} 
re{ n,}精確匹配n個前面表達式。
re{ n, m}匹配 n 到 m 次由前面的正則表達式定義的片段,貪婪方式
a| b匹配a或b
(re)G匹配括號內的表達式,也表示一個組
(?imx)正則表達式包含三種可選標誌:i, m, 或 x 。隻影響括號中的區域。
(?-imx)正則表達式關閉 i, m, 或 x 可選標誌。隻影響括號中的區域。
(?: re)類似 (...), 但是不表示一個組
(?imx: re)在括號中使用i, m, 或 x 可選標誌
(?-imx: re)在括號中不使用i, m, 或 x 可選標誌
(?#...)註釋.
(?= re)前向肯定界定符。如果所含正則表達式,以 ... 表示,在當前位置成功匹配時成功,否則失敗。但一旦所含表達式已經嘗試,匹配引擎根本沒有提高;模式的剩餘部分還要嘗試界定符的右邊。
(?! re)前向否定界定符。與肯定界定符相反;當所含表達式不能在字符串當前位置匹配時成功
(?> re)匹配的獨立模式,省去回溯。
\w匹配字母數字
\W匹配非字母數字
\s匹配任意空白字符,等價於 [\t\n\r\f].
\S匹配任意非空字符
\d匹配任意數字,等價於 [0-9].
\D匹配任意非數字
\A匹配字符串開始
\Z匹配字符串結束,如果是存在換行,只匹配到換行前的結束字符串。c
\z匹配字符串結束
\G匹配最後匹配完成的位置。
\b匹配一個單詞邊界,也就是指單詞和空格間的位置。例如, 'er\b' 可以匹配"never" 中的 'er',但不能匹配 "verb" 中的 'er'。
\B匹配非單詞邊界。'er\B' 能匹配 "verb" 中的 'er',但不能匹配 "never" 中的 'er'。
\n, \t, 等.匹配一個換行符。匹配一個製表符。等
\1...\9匹配第n個分組的子表達式。
\10匹配第n個分組的子表達式,如果它經匹配。否則指的是八進制字符碼的表達式。

正則表達式實例

字符匹配

實例描述
python匹配 "python".

字符類

實例描述
[Pp]ython匹配 "Python" 或 "python"
rub[ye]匹配 "ruby" 或 "rube"
[aeiou]匹配中括號內的任意一個字母
[0-9]匹配任何數字。類似於 [0123456789]
[a-z]匹配任何小寫字母
[A-Z]匹配任何大寫字母
[a-zA-Z0-9]匹配任何字母及數字
[^aeiou]除了aeiou字母以外的所有字符
[^0-9]匹配除了數字外的字符

特殊字符類

實例描述
.匹配除 "\n" 之外的任何單個字符。要匹配包括 '\n' 在內的任何字符,請使用象 '[.\n]' 的模式。
\d匹配一個數字字符。等價於 [0-9]。
\D匹配一個非數字字符。等價於 [^0-9]。
\s匹配任何空白字符,包括空格、製表符、換頁符等等。等價於 [ \f\n\r\t\v]。
\S匹配任何非空白字符。等價於 [^ \f\n\r\t\v]。
\w匹配包括下劃線的任何單詞字符。等價於'[A-Za-z0-9_]'。
\W匹配任何非單詞字符。等價於 '[^A-Za-z0-9_]'。

使用 X 選項寫成:

>>> rc
= re.compile(r"""
 

start a rule

/d+
     

   
        

number

|
[a-zA-Z]+
  

       

word

“”", re.VERBOSE)

在這個模式下,如果你想匹配一個空格,你必須用’/ ’
的形式(’/’
後面跟一個空格)

 

 

2.2 match 與 search

match( rule , targetString [,flag] )

search( rule , targetString [,flag] )

(注: re 的 match 與 search 函數同 compile 過的 Pattern 對象的 match 與 search 函數的參數是不一樣的。 Pattern 對象的 match 與 search 函數更爲強大,是真正最常用的函數)

按照規則在目標字符串中進行匹配。

第一個參數是正則規則,第二個是目標字符串,第三個是選項(同 compile 函數的選項)

返回:若成功返回一個 Match 對象,失敗無返回

findall 雖然很直觀,但是在進行更復雜的操作時,就有些力不從心了。此時更多的使用的是 match 和 search 函數。他們的參數和 findall 是一樣的,都是:

match( rule , targetString [,flag] )

search( rule , targetString [,flag] )

不過它們的返回不是一個簡單的字符串列表,而是一個 MatchObject (如果匹配成功的話) . 。通過操作這個 matchObject ,我們可以得到更多的信息。

需要注意的是,如果匹配不成功,它們則返回一個 NoneType 。所以在對匹配完的結果進行操作之前,你必需先判斷一下是否匹配成功了,比如:

>>> m=re.match( rule , target )

>>> if m:                       # 必需先判斷是否成功

        doSomethin

這兩個函數唯一的區別是: match 從字符串的開頭開始匹配,如果開頭位置沒有匹配成功,就算失敗了;而 search 會跳過開頭,繼續向後尋找是否有匹配的字符串。針對不同的需要,可以靈活使用這兩個函數。

關於 match 返回的 MatchObject 如果使用的問題,是 Python 正則式的精髓所在,它與組的使用密切相關。我將在下一部分詳細講解,這裏只舉個最簡單的例子:

例:

>>> s= ‘Tom:9527 , Sharry:0003’

>>> m=re.match( r’(?P<name>/w+)??P<num>/d+)’ , s )

>>> m.group()

‘Tom:9527’

>>> m.groups()

(‘Tom’, ‘9527’)

>>> m.group(‘name’)

‘Tom’

>>> m.group(‘num’)

‘9527’

 

2.3 finditer

finditer( rule , target [,flag] )

參數同 findall

返回一個迭代器

finditer 函數和 findall 函數的區別是, findall 返回所有匹配的字符串,並存爲一個列表,而 finditer 則並不直接返回這些字符串,而是返回一個迭代器。關於迭代器,解釋起來有點複雜,還是看看例子把:

>>> s=’111 222 333 444’

>>> for i in re.finditer(r’/d+’ , s ):

        print i.group(),i.span()          # 打印每次得到的字符串和起始結束位置

結果是

111 (0, 3)

222 (4, 7)

333 (8, 11)

444 (12, 15)

簡單的說吧,就是 finditer 返回了一個可調用的對象,使用 for i in finditer() 的形式,可以一個一個的得到匹配返回的 Match 對象。這在對每次返回的對象進行比較複雜的操作時比較有用。

 

2.4 字符串的替換和修改

re 模塊還提供了對字符串的替換和修改函數,他們比字符串對象提供的函數功能要強大一些。這幾個函數是

sub ( rule , replace , target [,count] )

subn(rule , replace , target [,count] )

在目標字符串中規格規則查找匹配的字符串,再把它們替換成指定的字符串。你可以指定一個最多替換次數,否則將替換所有的匹配到的字符串。

第一個參數是正則規則,第二個參數是指定的用來替換的字符串,第三個參數是目標字符串,第四個參數是最多替換次數。

這兩個函數的唯一區別是返回值。

sub 返回一個被替換的字符串

sub 返回一個元組,第一個元素是被替換的字符串,第二個元素是一個數字,表明產生了多少次替換。

例,將下面字符串中的 ’dog’ 全部替換成 ’cat’

>>> s=’ I have a dog , you have a dog , he have a dog ‘

>>> re.sub( r’dog’ , ‘cat’ , s )

’ I have a cat , you have a cat , he have a cat ‘

如果我們只想替換前面兩個,則

>>> re.sub( r’dog’ , ‘cat’ , s , 2 )

’ I have a cat , you have a cat , he have a dog ‘

或者我們想知道發生了多少次替換,則可以使用 subn

>>> re.subn( r’dog’ , ‘cat’ , s )

(’ I have a cat , you have a cat , he have a cat ‘, 3)

 

split( rule , target [,maxsplit] )

切片函數。使用指定的正則規則在目標字符串中查找匹配的字符串,用它們作爲分界,把字符串切片。

第一個參數是正則規則,第二個參數是目標字符串,第三個參數是最多切片次數

返回一個被切完的子字符串的列表

這個函數和 str 對象提供的 split 函數很相似。舉個例子,我們想把上例中的字符串被 ’,’ 分割開,同時要去掉逗號前後的空格

>>> s=’ I have a dog   ,   you have a dog  ,  he have a dog ‘

>>> re.split( ‘/s*,/s*’ , s )

[’ I have a dog’, ‘you have a dog’, ‘he have a dog ‘]

結果很好。如果使用 str 對象的 split 函數,則由於我們不知道 ’,’ 兩邊會有多少個空格,而不得不對結果再進行一次處理。

 

escape( string )

這是個功能比較古怪的函數,它的作用是將字符串中的 non-alphanumerics 字符(我已不知道該怎麼翻譯比較好了)用反義字符的形式顯示出來。有時候你可能希望在正則式中匹配一個字符串,不過裏面含有很多 re 使用的符號,你要一個一個的修改寫法實在有點麻煩,你可以使用這個函數 ,

例 在目標字符串 s 中匹配 ’(+?)’ 這個子字符串

>>> s= ‘111 222 (+?) 333’

>>> rule= re.escape( r’(+?)’ )

>>> print rule

/(//+/?/)

>>> re.findall( rule , s )

[’(+?)’]

 

3     更深入的瞭解 re 的組與對象

前面對 Python 正則式的組進行了一些簡單的介紹,由於還沒有介紹到 match 對象,而組又是和 match 對象密切相關的,所以必須將它們結合起來介紹才能充分地說明它們的用途。

不過再詳細介紹它們之前,我覺得有必要先介紹一下將規則編譯後的生成的 patter 對象

3.1 編譯後的 Pattern 對象

將一個正則式,使用 compile 函數編譯,不僅是爲了提高匹配的速度,同時還能使用一些附加的功能。編譯後的結果生成一個 Pattern 對象,這個對象裏面有很多函數,他們看起來和 re 模塊的函數非常象,它同樣有 findall , match , search ,finditer , sub , subn , split 這些函數,只不過它們的參數有些小小的不同。一般說來, re 模塊函數的第一個參數,即正則規則不再需要了,應爲規則就包含在 Pattern 對象中了,編譯選項也不再需要了,因爲已經被編譯過了。因此 re 模塊中函數的這兩個參數的位置,就被後面的參數取代了。

findall , match , search 和 finditer 這幾個函數的參數是一樣的,除了少了規則和選項兩個參數外,它們又加入了另外兩個參數,它們是:查找開始位置和查找結束位置,也就是說,現在你可以指定查找的區間,除去你不感興趣的區間。它們現在的參數形式是:

findall ( targetString [, startPos [,endPos] ] )

finditer ( targetString [, startPos [,endPos] ] )

match ( targetString [, startPos [,endPos] ] )

search ( targetString [, startPos [,endPos] ] )

這些函數的使用和 re 模塊的同名函數使用完全一樣。所以就不多介紹了。

 

除了和 re 模塊的函數同樣的函數外, Pattern 對象還多了些東西,它們是:

flags       查詢編譯時的選項

pattern 查詢編譯時的規則

groupindex 規則裏的組

這幾個不是函數,而是一個值。它們提供你一些規則的信息。比如下面這個例子

>>> p=re.compile( r’(?P<word>/b[a-z]+/b)|(?P<num>/b/d+/b)|(?P<id>/b[a-z_]+/w/b)’ , re.I )

>>> p.flags

2

>>> p.pattern

’(?P<word>//b[a-z]+//b)|(?P<num>//b//d+//b)|(?P<id>//b[a-z_]+//w*//b)’

>>> p.groupindex

{‘num’: 2, ‘word’: 1, ‘id’: 3}

我們來分析一下這個例子:這個正則式是匹配單詞、或數字、或一個由字母或 ’’ 開頭,後面接字母或數字的一個 ID 。我們給這三種情況的規則都包入了一個命名組,分別命名爲 ’word’ , ‘num’ 和 ‘id’ 。我們規定大小寫不敏感,所以使用了編譯選項 ‘I’ 。

編譯以後返回的對象爲 p ,通過 p.flag 我們可以查看編譯時的選項,不過它顯示的不是 ’I’ ,而是一個數值 2 。其實 re.I 是一個整數, 2 就是它的值。我們可以查看一下:

>>> re.I

2

>>> re.L

4

>>> re.M

8

每個選項都是一個數值。

通過 p.pattern 可以查看被編譯的規則是什麼。使用 print 的話會更好看一些

>>> print p.pattern

(?P<word>/b[a-z]+/b)|(?P<num>/b/d+/b)|(?P<id>/b[a-z]+/w*/b)

看,和我們輸入的一樣。

接下來的 p.groupindex 則是一個字典,它包含了規則中的所有命名組。字典的 key 是名字, values 是組的序號。由於字典是以名字作爲 key ,所以一個無命名的組不會出現在這裏。

 

 

3.2 組與 Match 對象

組與 Match 對象是 Python 正則式的重點。只有掌握了組和 Match 對象的使用,纔算是真正學會了 Python 正則式。

3.2.1 組的名字與序號

正則式中的每個組都有一個序號,它是按定義時從左到右的順序從 1 開始編號的。其實, re 的正則式還有一個 0 號組,它就是整個正則式本身。

我們來看個例子

>>> p=re.compile( r’(?P<name>[a-z]+)/s+(?P<age>/d+)/s+(?P<tel>/d+).’ , re.I )

>>> p.groupindex

{‘age’: 2, ‘tel’: 3, ‘name’: 1}

>>> s=’Tom 24 88888888  <=’

>>> m=p.search(s)

>>> m.groups()                           # 看看匹配的各組的情況

(‘Tom’, ‘24’, ‘8888888’)

>>> m.group(‘name’)                   # 使用組名獲取匹配的字符串

‘Tom’

>>> m.group( 1 )                         # 使用組序號獲取匹配的字符串,同使用組名的效果一樣

>>> m.group(0)                           # 0 組裏面是什麼呢?

‘Tom 24 88888888  <=‘

原來 0 組就是整個正則式 , 包括沒有被包圍到組裏面的內容。當獲取 0 組的時候,你可以不寫這個參數。 m.group(0) 和 m.group() 的效果是一樣的:

>>> m.group()

‘Tom 24 88888888  <=’

 

接下來看看更多的 Match 對象的方法,看看我們能做些什麼。

3.2.2 Match 對象的方法

group([index|id]) 獲取匹配的組,缺省返回組 0, 也就是全部值

groups()                返回全部的組

groupdict()            返回以組名爲 key ,匹配的內容爲 values 的字典

接上例:

>>> m.groupindex()

{‘age’: ‘24’, ‘tel’: ‘88888888’, ‘name’: ‘Tom’}

start( [group] )     獲取匹配的組的開始位置

end( [group] )              獲取匹配的組的結束位置

span( [group] )     獲取匹配的組的(開始,結束)位置

 

expand( template ) 根據一個模版用找到的內容替換模版裏的相應位置

這個功能比較有趣,它根據一個模版來用匹配到的內容替換模版中的相應位置,組成一個新的字符串返回。它使用 /g<index|name> 或 /index 來指示一個組。

接上例

>>> m.expand(r’name is /g<1> , age is /g<age> , tel is /3’)

‘name is Tom , age is 24 , tel is 88888888’

 

除了以上這些函數外, Match 對象還有些屬性

pos          搜索開始的位置參數

endpos   搜索結束的位置參數

這兩個是使用 findall 或 match 等函數時,傳入的參數。在上面這個例子裏,我們沒有指定開始和結束位置,那麼缺省的開始位置就是 0, 結束位置就是最後。

>>> m.pos

0

>>> m.endpos

19

lastindex 最後匹配的組的序號

>>> m.lastindex

3

lastgroup       最後匹配的組名

>>> m.lastgroup

’tel’

re     產生這個匹配的 Pattern 對象,可以認爲是個逆引用

>>> m.re.pattern

’(?P<name>[a-z]+)//s+(?P<age>//d+)//s+(?P<tel>//d+).'

得到了產生這個匹配的規則

string 匹配的目標字符串

>>> m.string

’Tom 24 88888888  <=‘


校驗數字的表達式


1. 數字:1KaTeX parse error: Undefined control sequence: \d at position 315: …3);">2. n位的數字:^\̲d̲{n}


3. 至少n位的數字:^\d{n,}KaTeX parse error: Undefined control sequence: \d at position 295: …;">4. m-n位的數字:^\̲d̲{m,n}


5. 零和非零開頭的數字:^(0|[1-9][0-9])</span></p><pstyle="clear:both;minheight:1em;color:rgb(62,62,62);"><spanstyle="lineheight:inherit;fontsize:15px;color:rgb(63,63,63);"><br></span></p><pstyle="clear:both;minheight:1em;color:rgb(62,62,62);"><spanstyle="lineheight:inherit;fontsize:15px;color:rgb(63,63,63);">6.([19][09])+(.[09]1,2)?</span></p><p style="clear:both;min-height:1em;color:rgb(62,62,62);"><span style="line-height:inherit;font-size:15px;color:rgb(63,63,63);"><br></span></p><p style="clear:both;min-height:1em;color:rgb(62,62,62);"><span style="line-height:inherit;font-size:15px;color:rgb(63,63,63);">6. 非零開頭的最多帶兩位小數的數字:^([1-9][0-9]*)+(.[0-9]{1,2})?


7. 帶1-2位小數的正數或負數:^(-)?\d+(.\d{1,2})?KaTeX parse error: Undefined control sequence: \- at position 298: …8. 正數、負數、和小數:^(\̲-̲|\+)?\d+(\.\d+)…


9. 有兩位小數的正實數:2+(.[0-9]{2})?</span></p><pstyle="clear:both;minheight:1em;color:rgb(62,62,62);"><spanstyle="lineheight:inherit;fontsize:15px;color:rgb(63,63,63);"><br></span></p><pstyle="clear:both;minheight:1em;color:rgb(62,62,62);"><spanstyle="lineheight:inherit;fontsize:15px;color:rgb(63,63,63);">10.1 3[09]+(.[09]1,3)?</span></p><p style="clear:both;min-height:1em;color:rgb(62,62,62);"><span style="line-height:inherit;font-size:15px;color:rgb(63,63,63);"><br></span></p><p style="clear:both;min-height:1em;color:rgb(62,62,62);"><span style="line-height:inherit;font-size:15px;color:rgb(63,63,63);">10. 有1~3位小數的正實數:^[0-9]+(.[0-9]{1,3})?


11. 非零的正整數:3\d*$ 或 ^([1-9][0-9]){1,3}$ 或 ^+?[1-9][0-9]KaTeX parse error: Undefined control sequence: \- at position 295: …;">12. 非零的負整數:^\̲-̲[1-9][]0-9"* 或 ^-[1-9]\d*KaTeX parse error: Undefined control sequence: \d at position 293: …3);">13. 非負整數:^\̲d̲+4\d*|0KaTeX parse error: Undefined control sequence: \d at position 299: …4. 非正整數:^-[1-9]\̲d̲*|0 或 ^((-\d+)|(0+))KaTeX parse error: Undefined control sequence: \d at position 294: …);">15. 非負浮點數:^\̲d̲+(\.\d+)?5\d*.\d*|0.\d*[1-9]\d*|0?.0+|0KaTeX parse error: Undefined control sequence: \d at position 297: …>16. 非正浮點數:^((-\̲d̲+(\.\d+)?)|(0+(… 或 ^(-([1-9]\d*.\d*|0.\d*[1-9]\d*))|0?.0+|0KaTeX parse error: Undefined control sequence: \d at position 298: …17. 正浮點數:^[1-9]\̲d̲*\.\d*|0\.\d*[1… 或 ^(([0-9]+.[0-9][1-9][0-9])|([0-9][1-9][0-9].[0-9]+)|([0-9][1-9][0-9]))KaTeX parse error: Undefined control sequence: \d at position 300: …. 負浮點數:^-([1-9]\̲d̲*\.\d*|0\.\d*[1… 或 ^(-(([0-9]+.[0-9][1-9][0-9])|([0-9][1-9][0-9].[0-9]+)|([0-9][1-9][0-9])))KaTeX parse error: Undefined control sequence: \d at position 295: …;">19. 浮點數:^(-?\̲d̲+)(\.\d+)? 或 ^-?([1-9]\d*.\d*|0.\d*[1-9]\d*|0?.0+|0)KaTeX parse error: Can't use function '\u' in math mode at position 672: …,63);">1. 漢字:^[\̲u̲4e00-\u9fa5]{0,…


2. 英文和數字:6+$ 或 7{4,40}</span></p><pstyle="clear:both;minheight:1em;color:rgb(62,62,62);"><spanstyle="fontsize:15px;color:rgb(63,63,63);"><br></span></p><pstyle="clear:both;minheight:1em;color:rgb(62,62,62);"><spanstyle="fontsize:15px;color:rgb(63,63,63);">3.320.3,20</span></p><p style="clear:both;min-height:1em;color:rgb(62,62,62);"><span style="font-size:15px;color:rgb(63,63,63);"><br></span></p><p style="clear:both;min-height:1em;color:rgb(62,62,62);"><span style="font-size:15px;color:rgb(63,63,63);">3. 長度爲3-20的所有字符:^.{3,20}


4. 由26個英文字母組成的字符串:8+</span></p><pstyle="clear:both;minheight:1em;color:rgb(62,62,62);"><spanstyle="fontsize:15px;color:rgb(63,63,63);"><br></span></p><pstyle="clear:both;minheight:1em;color:rgb(62,62,62);"><spanstyle="fontsize:15px;color:rgb(63,63,63);">5.26[AZ]+</span></p><p style="clear:both;min-height:1em;color:rgb(62,62,62);"><span style="font-size:15px;color:rgb(63,63,63);"><br></span></p><p style="clear:both;min-height:1em;color:rgb(62,62,62);"><span style="font-size:15px;color:rgb(63,63,63);">5. 由26個大寫英文字母組成的字符串:^[A-Z]+


6. 由26個小寫英文字母組成的字符串:9+</span></p><pstyle="clear:both;minheight:1em;color:rgb(62,62,62);"><spanstyle="fontsize:15px;color:rgb(63,63,63);"><br></span></p><pstyle="clear:both;minheight:1em;color:rgb(62,62,62);"><spanstyle="fontsize:15px;color:rgb(63,63,63);">7.26[AZaz09]+</span></p><p style="clear:both;min-height:1em;color:rgb(62,62,62);"><span style="font-size:15px;color:rgb(63,63,63);"><br></span></p><p style="clear:both;min-height:1em;color:rgb(62,62,62);"><span style="font-size:15px;color:rgb(63,63,63);">7. 由數字和26個英文字母組成的字符串:^[A-Za-z0-9]+


8. 由數字、26個英文字母或者下劃線組成的字符串:^\w+$ 或 ^\w{3,20}KaTeX parse error: Can't use function '\u' in math mode at position 262: …文、英文、數字包括下劃線:^[\̲u̲4E00-\u9FA5A-Za…


10. 中文、英文、數字但不包括下劃線等符號:10+$ 或 11{2,20}KaTeX parse error: Expected group after '^' at position 253: …3);">11. 可以輸入含有^̲%&amp;',;=?"等字符:[^%&’,;=?KaTeX parse error: Undefined control sequence: \x at position 1: \̲x̲22]+ 12 禁止輸入含有~…


2. 域名:[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(/.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+/.?


3. InternetURL:[a-zA-z]+://[^\s]* 或 ^http://([\w-]+.)+[\w-]+(/[\w-./?%&=])?KaTeX parse error: Undefined control sequence: \d at position 256: …2|3|5|6|7|8|9])\̲d̲{8}


5. 電話號碼(“XXX-XXXXXXX”、“XXXX-XXXXXXXX”、“XXX-XXXXXXX”、“XXX-XXXXXXXX”、"XXXXXXX"和"XXXXXXXX):^((\d{3,4}-)|\d{3.4}-)?\d{7,8}KaTeX parse error: Undefined control sequence: \d at position 280: …、021-87888822):\̲d̲{3}-\d{8}|\d{4}…


8. 短身份證號碼(數字、字母x結尾):^([0-9]){7,18}(x|X)?$ 或 ^\d{8,18}|[0-9x]{8,18}|[0-9X]{8,18}?</span></p><pstyle="clear:both;minheight:1em;color:rgb(62,62,62);"><spanstyle="fontsize:15px;color:rgb(63,63,63);"><br></span></p><pstyle="clear:both;minheight:1em;color:rgb(62,62,62);"><spanstyle="fontsize:15px;color:rgb(63,63,63);">9.(516)[azAZ][azAZ09]4,15</span></p><p style="clear:both;min-height:1em;color:rgb(62,62,62);"><span style="font-size:15px;color:rgb(63,63,63);"><br></span></p><p style="clear:both;min-height:1em;color:rgb(62,62,62);"><span style="font-size:15px;color:rgb(63,63,63);">9. 帳號是否合法(字母開頭,允許5-16字節,允許字母數字下劃線):^[a-zA-Z][a-zA-Z0-9_]{4,15}


10. 密碼(以字母開頭,長度在6~18之間,只能包含字母、數字和下劃線):12\w{5,17}KaTeX parse error: Undefined control sequence: \d at position 293: …在8-10之間):^(?=.*\̲d̲)(?=.*[a-z])(?=…


12. 日期格式:^\d{4}-\d{1,2}-\d{1,2}


13. 一年的12個月(01~09和1~12):^(0?[1-9]|1[0-2])</span></p><pstyle="clear:both;minheight:1em;color:rgb(62,62,62);"><spanstyle="fontsize:15px;color:rgb(63,63,63);"><br></span></p><pstyle="clear:both;minheight:1em;color:rgb(62,62,62);"><spanstyle="fontsize:15px;color:rgb(63,63,63);">14.31(0109131)((0?[19])((12)[09])3031)</span></p><p style="clear:both;min-height:1em;color:rgb(62,62,62);"><span style="font-size:15px;color:rgb(63,63,63);"><br></span></p><p style="clear:both;min-height:1em;color:rgb(62,62,62);"><span style="font-size:15px;color:rgb(63,63,63);">14. 一個月的31天(01~09和1~31):^((0?[1-9])|((1|2)[0-9])|30|31)


15. 錢的輸入格式:


16. 1.有四種錢的表示形式我們可以接受:“10000.00” 和 “10,000.00”, 和沒有 “分” 的 “10000” 和 “10,000”:13[0-9]</span></p><pstyle="clear:both;minheight:1em;color:rgb(62,62,62);"><spanstyle="fontsize:15px;color:rgb(63,63,63);"><br></span></p><pstyle="clear:both;minheight:1em;color:rgb(62,62,62);"><spanstyle="fontsize:15px;color:rgb(63,63,63);">17.2.0,,"0",(0[19][09])</span></p><p style="clear:both;min-height:1em;color:rgb(62,62,62);"><span style="font-size:15px;color:rgb(63,63,63);"><br></span></p><p style="clear:both;min-height:1em;color:rgb(62,62,62);"><span style="font-size:15px;color:rgb(63,63,63);">17. 2.這表示任意一個不以0開頭的數字,但是,這也意味着一個字符"0"不通過,所以我們採用下面的形式:^(0|[1-9][0-9]*)


18. 3.一個0或者一個不以0開頭的數字.我們還可以允許開頭有一個負號:^(0|-?[1-9][0-9])</span></p><pstyle="clear:both;minheight:1em;color:rgb(62,62,62);"><spanstyle="fontsize:15px;color:rgb(63,63,63);"><br></span></p><pstyle="clear:both;minheight:1em;color:rgb(62,62,62);"><spanstyle="fontsize:15px;color:rgb(63,63,63);">19.4.00.0.,.[09]+(.[09]+)?</span></p><p style="clear:both;min-height:1em;color:rgb(62,62,62);"><span style="font-size:15px;color:rgb(63,63,63);"><br></span></p><p style="clear:both;min-height:1em;color:rgb(62,62,62);"><span style="font-size:15px;color:rgb(63,63,63);">19. 4.這表示一個0或者一個可能爲負的開頭不爲0的數字.讓用戶以0開頭好了.把負號的也去掉,因爲錢總不能是負的吧.下面我們要加的是說明可能的小數部分:^[0-9]+(.[0-9]+)?


20. 5.必須說明的是,小數點後面至少應該有1位數,所以"10."是不通過的,但是 “10” 和 “10.2” 是通過的:14+(.[0-9]{2})?</span></p><pstyle="clear:both;minheight:1em;color:rgb(62,62,62);"><spanstyle="fontsize:15px;color:rgb(63,63,63);"><br></span></p><pstyle="clear:both;minheight:1em;color:rgb(62,62,62);"><spanstyle="fontsize:15px;color:rgb(63,63,63);">21.6.,,[09]+(.[09]1,2)?</span></p><p style="clear:both;min-height:1em;color:rgb(62,62,62);"><span style="font-size:15px;color:rgb(63,63,63);"><br></span></p><p style="clear:both;min-height:1em;color:rgb(62,62,62);"><span style="font-size:15px;color:rgb(63,63,63);">21. 6.這樣我們規定小數點後面必須有兩位,如果你認爲太苛刻了,可以這樣:^[0-9]+(.[0-9]{1,2})?


22. 7.這樣就允許用戶只寫一位小數.下面我們該考慮數字中的逗號了,我們可以這樣:15{1,3}(,[0-9]{3})(.[0-9]{1,2})?</span></p><pstyle="clear:both;minheight:1em;color:rgb(62,62,62);"><spanstyle="fontsize:15px;color:rgb(63,63,63);"><br></span></p><pstyle="clear:both;minheight:1em;color:rgb(62,62,62);"><spanstyle="fontsize:15px;color:rgb(63,63,63);">238.13,+3,,([09]+[09]1,3(,[09]3))(.[09]1,2)?</span></p><p style="clear:both;min-height:1em;color:rgb(62,62,62);"><span style="font-size:15px;color:rgb(63,63,63);"><br></span></p><p style="clear:both;min-height:1em;color:rgb(62,62,62);"><span style="font-size:15px;color:rgb(63,63,63);">23 8.1到3個數字,後面跟着任意個 逗號+3個數字,逗號成爲可選,而不是必須:^([0-9]+|[0-9]{1,3}(,[0-9]{3})*)(.[0-9]{1,2})?


24. 備註:這就是最終結果了,別忘了"+“可以用”"替代如果你覺得空字符串也可以接受的話(奇怪,爲什麼?)最後,別忘了在用函數時去掉去掉那個反斜槓,一般的錯誤都在這裏


25. xml文件:^([a-zA-Z]±?)+[a-zA-Z0-9]+\.[x|X][m|M][l|L]KaTeX parse error: Can't use function '\u' in math mode at position 259: …6. 中文字符的正則表達式:[\̲u̲4e00-\u9fa5]</s…或(^\s)|(\s*$) (可以用來刪除行首行尾的空白字符(包括空格、製表符、換頁符等等),非常有用的表達式)


31. 騰訊QQ號:[1-9][0-9]{4,} (騰訊QQ號從10000開始)


32. 中國郵政編碼:[1-9]\d{5}(?!\d) (中國郵政編碼爲6位數字)


33. IP地址:\d+.\d+.\d+.\d+ (提取IP地址時有用)


34. IP地址:((???:25[0-5]|2[0-4]\d|[01]?\d?\d)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d?\d))



  1. 0-9 ↩︎

  2. 0-9 ↩︎

  3. 1-9 ↩︎

  4. 1-9 ↩︎

  5. 1-9 ↩︎

  6. A-Za-z0-9 ↩︎

  7. A-Za-z0-9 ↩︎

  8. A-Za-z ↩︎

  9. a-z ↩︎

  10. \u4E00-\u9FA5A-Za-z0-9 ↩︎

  11. \u4E00-\u9FA5A-Za-z0-9 ↩︎

  12. a-zA-Z ↩︎

  13. 1-9 ↩︎

  14. 0-9 ↩︎

  15. 0-9 ↩︎

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