The Python module's re-regular expression

Source: Internet
Author: User

41, Python regular expression 1,

Python in the RE module provides regular expression-related operations

Character:

. Match any character other than line break
\w match letters or numbers or underscores or kanji
\s matches any whitespace character
\d Matching numbers
\b Match the beginning or end of a word
^ Start of matching string
$ match End of string

Number:

* Repeat 0 or more times
+ Repeat one or more times
? Repeat 0 or one time
{n} repeats n times
{n,} repeats n or more times
{N,m} repeats n to M times

Match

12345678910111213141516171819202122232425 # match,从起始位置开始匹配,匹配成功返回一个对象,未匹配成功返回None match(pattern, string, flags=0) # pattern: 正则模型 # string : 要匹配的字符串 # falgs  : 匹配模式     X  VERBOSE     Ignore whitespace andcomments fornicer looking RE‘s.     I  IGNORECASE  Perform case-insensitive matching.     M  MULTILINE   "^"matches the beginning of lines (after a newline)                    as well as the string.                    "$"matches the end of lines (before a newline) as well                    as the end of the string.     S  DOTALL      "."matches anycharacter at all, including the newline.     A  ASCII       For string patterns, make \w, \W, \b, \B, \d, \D                    match the corresponding ASCII character categories                    (rather than the whole Unicodecategories, which isthe                    default).                    For bytes patterns, this flag isthe only available                    behaviour andneedn‘t be specified.          L  LOCALE      Make \w, \W, \b, \B, dependent on the current locale.     U  UNICODEFor compatibility only. Ignored forstring patterns (it                    isthe default), andforbidden forbytes patterns.
# no grouping R = Re.match ("h\w+", origin) print (R.group ()) # gets all the results that match to print (R.groups ()) # Gets the model that matches to the Group result print (R.groupdict ()) # Gets the grouped results that match in the model # There are groups # Why do you have to group? Extracts the specified content that matches successfully (first matches all the regular, then matches the successful local content extracted) R = Re.match ("H (\w+)." p<name>\d) $ ", origin) print (R.group ()) # gets all the results that match to print (R.groups ()) # Gets the grouped results that match into the model PR Int (r.groupdict ()) # gets all the groups in the model that have executed the key in the matched group Demo

Search

12 # search,浏览整个字符串去匹配第一个,未匹配成功返回None# search(pattern, string, flags=0)
# no grouping R = Re.search ("a\w+", origin) print (R.group ()) # gets all results matching to print (R.groups ()) # Gets the match in the model The grouped result to print (R.groupdict ()) # Gets the grouped result that matches to in the model # there are groups R = Re.search ("A (\w+)." p<name>\d) $ ", origin) print (R.group ()) # gets all the results that match to print (R.groups ()) # Gets the grouped results that match into the model PR Int (r.groupdict ()) # gets all the groups in the model that have executed the key in the matched group demo

FindAll

123 # findall,获取非重复的匹配列表;如果有一个组则以列表形式返回,且每一个匹配均是字符串;如果模型中有多个组,则以列表形式返回,且每一个匹配均是元祖;# 空的匹配也会包含在结果中#findall(pattern, string, flags=0)
# no grouping R = Re.findall ("a\w+", origin) print (r) # has grouping origin = "Hello Alex bcd abcd Lge ACD 19" r = Re.findall ("A ((\w*) c) (d)", origin) print (R) Demo

Sub

12345678 # sub,替换匹配成功的指定位置字符串sub(pattern, repl, string, count=0, flags=0)# pattern: 正则模型# repl   : 要替换的字符串或可执行对象# string : 要匹配的字符串# count  : 指定匹配个数# flags  : 匹配模式
        # unrelated to grouping        origin = "Hello Alex bcd Alex Lge" Alex ACD "        r = re.sub (" a\w+ "," 999 ", origin, 2)        print (R)

Split

1234567 # split,根据正则匹配分割字符串split(pattern, string, maxsplit=0, flags=0)# pattern: 正则模型# string : 要匹配的字符串# maxsplit:指定分割个数# flags  : 匹配模式
        # no grouping        origin = "Hello Alex bcd Alex Lge Alex ACD"        r = Re.split ("Alex", origin, 1)        print (R)        # with Group 
   origin = "Hello Alex bcd Alex Lge" Alex ACD "        r1 = Re.split (" (Alex) ", origin, 1)        print (r1)        r2 = Re.spli T ("(Al (ex))", origin, 1)        print (R2)
Ip:
^ (25[0-5]|2[0-4]\d| [0-1]?\d?\d) (\. ( 25[0-5]|2[0-4]\d| [0-1]?\d?\d)] {3}$
Phone Number:
^1[3|4|5|8][0-9]\d{8}$
Mailbox:
[a-za-z0-9_-] [Email protected] [A-za-z0-9_-]+ (\.[ a-za-z0-9_-]+) +

The Python module's re-regular expression

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.