Python RE Module

Source: Internet
Author: User

Re.match function

Re.match attempts to match a pattern from the starting position of the string, and if the match is not successful, match () returns none.

def match (self, string, pos=0, Endpos=-1):
"" "Matches Zero | More characters at the beginning of the string.

import renum = re.compile(r‘(\d{4})-(\d{7})‘)result = num.match(‘my number is 0310-5561921‘)print(result)print(‘#‘*30)num_1 = re.compile(r‘\w*(\d{4})-(\d{7})‘)result = num_1.match(‘mynumberis0310-5561921‘)print(result.groups())print(result.group(1))print(result.group(2))运行:C:\Python27\python.exe D:/Python/modules/Re.pyNone##############################(‘0310‘, ‘5561921‘)03105561921
Re.search method

Re.search scans the entire string and returns the first successful match.

num = re.compile(r‘(\d{4})-(\d{7})‘)result_match = num.match(_str)result_search = num.search(_str)print(‘mache : {0}‘.format(result_match))print(‘search : {0}‘.format(result_search.group()))运行:C:\Python27\python.exe D:/Python/modules/Re.pymache : Nonesearch : 0310-5561921
_str = ‘mynumberis0310-5561921‘num_1 = re.compile(r‘\w*(\d{4})-(\d{7})‘)result_match = num_1.match(_str)result_search = num_1.search(_str)# print(‘match.groups:{0}‘.format(result_match.groups()))print(‘match.group(1):{0}‘.format(result_match.group(1)))print(‘match.group(2):{0}‘.format(result_match.group(2)))print(‘search.groups():{0}‘.format(result_search.groups()))print(‘search.group(1):{0}‘.format(result_search.group(1)))print(‘search.group(2):{0}‘.format(result_search.group(2)))运行:match.group(1):0310match.group(2):5561921search.groups():(‘0310‘, ‘5561921‘)search.group(1):0310search.group(2):5561921
The difference between Re.match and Re.search

Re.match matches only the beginning of the string, if the string does not begin to conform to the regular expression, the match fails, the function returns none, and Re.search matches the entire string until a match is found.

Re.findall function

Find all the substrings that the re matches and return them as a list. This match is returned from left to right in an orderly manner. If there is no match, returns an empty column

_str = ‘ha1ha222ha3ha4ha5‘num = re.compile(r‘\d‘)print(num.findall(_str))运行:[‘1‘, ‘2‘, ‘2‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘]
Re.finditer function

Find all the substrings that the re matches and return them as an iterator. This match is returned from left to right in an orderly manner. If there is no match, returns an empty list

_str = ‘ha1ha222ha3ha4ha5‘num = re.compile(r‘\d‘)print(num.finditer(_str))for i in num.finditer(_str):    print(i)    print(i.group())    运行:<callable-iterator object at 0x00000000037C6DD8><_sre.SRE_Match object at 0x00000000037236B0>1<_sre.SRE_Match object at 0x0000000003723718>2<_sre.SRE_Match object at 0x00000000037236B0>2<_sre.SRE_Match object at 0x0000000003723718>2<_sre.SRE_Match object at 0x00000000037236B0>3<_sre.SRE_Match object at 0x0000000003723718>4<_sre.SRE_Match object at 0x00000000037236B0>5
Re.split function

Separates a string from a regular expression. If you enclose the regular expression in parentheses, the matching string is also returned in the list. Maxsplit is the number of separations, the maxsplit=1 is separated once, the default is 0, the number of times is not limited.

_str = ‘ha1ha222ha3ha4ha5‘result_split = re.split(r‘\d*‘, _str)print(result_split)运行:[‘ha‘, ‘ha‘, ‘ha‘, ‘ha‘, ‘ha‘, ‘‘]

Python RE Module

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.