python-Module-re Regular expression

Source: Internet
Author: User

Problem: Remove the phone number from the file below

or the following:

Using regular expressions can be a bit simpler,

RE Module

The regular expression is the string matching rules, in most programming languages have corresponding support, Python corresponding module is re

Common expression Rules
'.'default match any character except \ n, if flag Dotall is specified, matches any character, including line break'^'Match the beginning of the character, if you specify the flags MULTILINE, this can also match on (R"^a","\nabc\neee", flags=Re. MULTILINE)'$'Matches the end of the character, if specified by flags MULTILINE, Re.search ('foo.$','foo1\nfoo2\n', Re. MULTILINE). Group () will match to Foo1'*'Matches the character preceding the * number 0 or more times, Re.search ('A *','Aaaabac') Results'AAAA''+'Matches the previous character 1 or more times, Re.findall ("ab+","Ab+cd+abb+bba"Results'AB','ABB']'?'Matches the previous character 1 or 0 times, Re.search ('b?','Alex'). Group () match B 0 Times'{m}'Matches the previous character m times, Re.search ('B{3}','Alexbbbs'). Group () matches to'BBB''{n,m}'Matches the previous character N to M times, Re.findall ("ab{1,3}","ABB ABC abbcbbb") Results'ABB','AB','ABB']'|'Match | left or | Right character, re.search ("abc| ABC","ABCBABCCD"). Group () results'ABC''(...)'Group matching, Re.search ("(ABC) {2}A (123|45)","abcabca456c"). Group () Results for'Abcabca45''\a'Match only from the beginning of the character, Re.search ("\AABC","ALEXABC") is not matched, equivalent to Re.match ('ABC',"ALEXABC") or ^'\z'match character end, same as $'\d'Match number 0-9'\d'match non-numeric'\w'Match [a-za-z0-9]'\w'Match non-[a-za-z0-9]'s'Match whitespace characters, \ t, \ n, \ r, Re.search ("\s+","ab\tc1\n3"). Group () results'\ t''(? P<name>, ...)'Group Matching Re.search ("(? P<province>[0-9]{4}) (? P<city>[0-9]{2}) (? P<BIRTHDAY>[0-9]{4})","371481199306143242"). Groupdict (" City"Results'Province':'3714',' City':'Bayi','Birthday':'1993'}

>>>ImportRe#.>>> Re.search ('.','Da854da')<_sre. Sre_match object; span= (0, 1), match='D'>#^>>> Re.search ('^a','AVCB')<_sre. Sre_match object; span= (0, 1), match='a'>#These two are the same.>>> Re.match ('av','AVCB')<_sre. Sre_match object; span= (0, 2), match='av'>#$ requires string to end with B>>> Re.search ('b$','AVCB')<_sre. Sre_match object; Span= (3, 4), match='b'>#if match is required to start with B and end with B, there is only one character ' B '

The matching syntax for re has the following types
    • Re.match match from the beginning
    • Re.search Match contains
    • Re.findall all matching characters to the elements in the list to return
    • Re.split as a list separator with matched characters
    • Re.sub match characters and replace
    • Re.fullmatch all matches
>>>ImportRe>>> s ='abc1d3c'>>> Re.match ('[0-9]', s) # Match from the beginning, match one character>>>Print(Re.match ('[0-9]', s)) #相当于函数, no match, it will return to Nonenone>>> Re.match ('[0-9]','1BDFD')<_sre. Sre_match object; span= (0, 1), match='1'>>>> Re.match ('[0-9]','115BDFD')<_sre. Sre_match object; span= (0, 1), match='1'>

Above with match cannot match to the required characters, below is search, this is the global searching, found on the return

Import Re ' abc1d3c '>>> re.search ('[0-9]', s)<_sre. Sre_match object; Span= (3, 4), match='1'># (3,4) is an index, referring to 3 to 4
>>> re.search (' [0-9] ', s). Group () #加上group就能拿到结果
' 1 '

So the correct approach should be the line to determine the return value is not, some words to take

Match_res = Re.search ('[0-9]', s)if  match_res:    Print (Match_res.group ())# get the match results

But neither of the above can get the numbers, so use the findall.

Import re>>> re.findall ('[0-9]', s) ['1'  '3']

Where FindAll returns a list, and search returns an object

python-Module-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.