淺學python | 正則表達式集錦

learningyard學苑 發佈 2022-12-18T10:14:55.655358+00:00

分享興趣,傳播快樂,增長見聞,留下美好!親愛的您,這裡是LearningYard學苑。歡迎大家繼續訪問學苑內容,今天小編為大家帶來有關Python的知識。

分享興趣,傳播快樂,增長見聞,留下美好!親愛的您,這裡是LearningYard學苑。歡迎大家繼續訪問學苑內容,今天小編為大家帶來有關Python的知識。

Share interest, spread happiness, increase knowledge, leave a good! Dear you, here is the LearningYard Academy. Welcome to continue to visit the content of the academy, today xiaobian to bring you knowledge about Python management


(1)最簡單的正則表達式是普通字符事,只能西配自身。

(2)'[pjc]ython'可以四配'python',jython',cython'

(3)[a-zA-Z0-9]'可以西配一個任意大小寫字用或數字。(4)'[^abe]可以一個四配任意除'a'、'b'、'c'之外的字符。

(5)'pythonlperl'或'p(ython|erl)'都可以匹配'python'或'perl'。

(6)r'(http://)? (wwwl.)? python\.org'只他 匹配http://www.python.org"http://python.org'、www,python.org和'python.org』.

(7)'*http'只能四配所有以'http『開頭的字符串。

(8)(pattern)*:允許模式重複0次或多次。

(9)(pattern)+:允許模式重複一次或多次。

(10)(pattern)(m,n}:允許模式重複m~n次,注意逗號後面不要有空格。

(11)'(alb)*c':匹配多個(包含0個)a或b,後面緊跟一個字母 c。

(12)』abl1,)':等價於'ab+',匹配以字母a開頭後面緊跟一個或多個字母b的字符串。

(13)『^[a-zA-Z]{1)([a-zA-Z0-9,_])(4,19}$':匹配長度為5~20的字符串,必須以字母開頭並且後面可帶數字、字母、「_」「.」的字符串。

(14)'^(\w)(6,20}$':匹配長度為6~20的字符串,可以包含字母、數字、下畫線。

(15) '^\d{1,3)\.\d(1.3)\.\d(1,3)\\d{1,3)$':檢查給定字符串是否為合法

(1) The simplest regular expression is a common character thing, which can only match itself in the west.

(2) '[pjc] ython' can be quadruple with 'python', jython ', cython'

(3) [A-zA-Z0-9]'can be paired with a number or number.(4) '[^ abe] can be matched with four characters other than' a ',' b ',' c '.

(5) Either 'pythonlperl' or 'p (ython | erl)' can match 'python' or 'perl'.

(6)r'(http://)?(wwwl.)?python\. The org 'only he matches the http://www.python.org"http://python.org', www,python.org and the 'python.org'.

(7) '* http' can only match all strings beginning with 'http'.

(8) (pattern) *: Allow the mode to repeat 0 or more times.

(9) (pattern) +: Allow the mode to be repeated once or more times.

(10) (pattern) (m, n}: allow the mode to repeat m~n times, note that there is no space behind the comma.

(11) '(alb) * c': Matches multiple (including 0) a or b, followed by one letter c.

(12) 'abl1,)': Equivalent to 'ab +', matching a string beginning with a letter b or more followed by a letter a.

(13) '^ [a-zA-Z] {1) ([a-zA-Z0-9, _]) (4,19} $': A matching string of 5 to 20 must start with a letter followed by numbers, letters, "_"" The string of.

(14) '^ (\ w) (6,20} $': Matches length 6 to 20 strings, can include letters, numbers, lines.

(15) '^\d{1,3)\. \d(1.3)\. \ d (1,3) \ \ d {1,3) $ ': Check if a given string is legal


使用時要注意的是,正則表達式只是進行形式上的檢查,並不保證內容一定正確。例如上面的例子中,正則表達式'^\d{1,3}\.\d{1,3}\.\d{1.3)\.\d(1,3)$可以檢查字符串是否為IP位址,字符串888.888.888.888這樣的也能通過檢查,但實際上並不是有效的IP位址。同樣的道理,,正則表達式'^\d{18}|\d(15)$'也只負責檢查字符串是否為18位或15位數字,並不保證一定是合法的身份證號。

It should be noted that regular expressions are only formal checks and do not guarantee that the content is correct. For example, in the example above, the regular expression'^ \ d {1,3} \. \d{1,3}\. \d{1.3)\. \ d (1,3) $ can check if the string is an IP address, and the string 888.888.888.88.888 also passes it, but it is not actually a valid IP address. In the same way, the regular expression '^ \ d {18} | \ d (15) $' is also responsible for checking only whether the string is in 18 or 15 digits, and does not necessarily guarantee a legitimate ID number.

今天的分享就到這兒了

如果您對今天的文章有獨特的想法

歡迎給我們留言

讓我們相約明天

祝您今天過得開心快樂!

That's all for today's sharing

If you have unique ideas about today's article

Feel free to leave us a comment

Let's meet for tomorrow

I wish you a happy life today!


翻譯:金山詞霸

本文由Learning Yard新學苑原創,

如有侵權請聯繫溝通~


文案 | 鄧粵慧

排版 | 鄧粵慧

審核 | 百味

關鍵字: