Python Regular Expressions

Source: Internet
Author: User

1. Find the pattern in text: Re.search ()
Import"you ispython"= Re.search ('is'  , text)#  Returns the position of the first character of ' is ' in text, note that it is starting from 0 print(Match.start ())  #  Returns the position of the last character of ' is ' in text, which is starting from 1 print(Match.end ())

Results:

47
2. Compile expression: Compile ()
ImportRetext="You are SB"
# The Compile () function converts an expression string into a regexobject
Regexes={re.compile (P) forPinch['SB','NB','MB'] } forRegexinchregexes:ifRegex.search (text):Print('seeking'+ Regex.pattern +'>> Match') Else: Print('seeking'+ Regex.pattern +'>> not match')

Results:

not >> not match

3. Multiple matches: FindAll (), Finditer ()

The FindAll () function returns all strings that match the pattern and do not overlap.

Import"ab aa ab bb ab ba"
# Match all of the ' ab ' combinations of text to print(Re.findall ('ab', text))

Results:

[' ab ' ab ' ab ' ]

Finditer () returns an iterator

Import"ab aa ab bb ab ba" for in Re.finditer ('  AB', text):    = match.start ()    = match.end ()      Print('found {} at {} to {}'. Format ('"ab', STR (s), str (e)))

Results:

" AB  " at 0 to 2"ab "@ 6 to 8"ab "at 14

4. Pattern syntax
ImportRedefTest_patterns (Text,patterns = []):    #The pattern returns the first element of the patterns item, and DESC returns the second element of the patterns item, respectively.    #This function is equivalent to patterns the first item as a match condition to match the text     forPattern,descinchPatterns:Print('Matching method:'+ Pattern,'Description:'+desc)Print('Text:'+text) forMatchinchRe.finditer (pattern,text): s=Match.start () e=match.end ()Print('found {} at {} to {}, result:{}'. Format (Pattern,str (s), str (e), (text[s:e)))

Execute the function:

# matches all A, followed by 1 B.
>>> test_patterns (' ', [('ab','A Followed by B')])
2 to 45 to 79 to one, Result:ab>>>

Repeat

There are 5 methods in the pattern that represent repetition, and now one by one show:

#* indicates that the pattern repeats 0 or more times (repeating 0 times means that it does not appear and can be matched)#matches a start, followed by 0 or more B, that is, as long as a can match>>> Test_patterns ('a AC ab ABB ABBB',[('ab*','a followed by zero or more B')]) matching method: AB* Description: A followed by zeroorMore B text: A AC ab ABB ABBB found AB* At 0 to 1, Result:afound AB* At 2 to 3, Result:afound AB* At 5 to 7, Result:abfound AB* at 8 to 11, Result:abbfound AB* at 16, RESULT:ABBB>>>

 #   + indicates that the pattern appears at least once  #   
>>> test_patterns ( '

#The representation pattern appears 0 or 1 times#matches a start, followed by 0 or 1 B, that is, as long as a can match>>> Test_patterns ('a ab ABB abbb',[('ab+','a followed by one or more B')]) matching method: AB+ Description: A followed by oneorMore B text: A AB ABB ABBB found AB+ at 2 to 4, Result:abfound AB+ at 5 to 8, Result:abbfound AB+ at 9 to 13, RESULT:ABBB>>>

# match a start, followed by 3 b>>> test_patterns (', ['ab{3}' ,'a followed by three B')])
Matching method: ab{3} Description: A followed by three B text: A ab ABB abbb found ab{3} at 9 to, result:abbb>> ;>

# match a start, followed by 2 or 3 b>>> test_patterns ('', [('ab{2,3} 'A followed by or three B')]) match: ab{or  three B Text: A ab ABB abbb found ab{2,3} at 5 to 8, Result:abbfound ab{2,3} at 9 to, result:abbb> >>

Character

A character set is a set of characters that contain all the characters that can match the corresponding position in the pattern, such as [AB] to match A or b.

# match all A or B>>> test_patterns ('abca', [('[ab]' ,'either a or b'or11 to 23 to 4, result:a>>>

# match A to the beginning, followed by a or B >>> test_patterns ('a aa ab ac', [('A[ab]  ','a followed by a or B'or2 to 4  5 to 7, Result:ab>>>

# match a start, followed by 1 or more A or B >>> test_patterns ('a aaaaa abbb ACCC', [(' a[ab]+ 'A followed by 1 or more A or B')])) matches by: A[ab]oror
     B Text: A AAAAA abbb acccfound A[ab]+ at 2 to 7, Result:aaaaafound A[ab]+at 8 to, result:a BBB>>>

Python Regular Expressions

Related Article

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.