"Python" Python and regular re are the main methods used to enumerate

Source: Internet
Author: User

"Directly on the code"

#coding =utf-8
#1, first compile the string form of the regular expression into a pattern instance

#2, using the pattern instance to process text and get matching results

#3, finally get the message using the match instance, and do other things

Import re

# "1" Re.compile (String[,flag]) compiles regular expressions into pattern objects

‘‘‘
# "2" Re.match (Pattern,string[,flags])
# try to match pattern starting at the beginning of the input parameter string
# encounters an unmatched character or has reached the end of a string, returns none immediately, and instead gets the result of the match

#将正则表达式编译成pattern对象
Pattern = Re.compile (R ' \d+ ')
#使用re. Match matches the text, gets the match result, cannot match when will return none
RESULT1 = Re.match (pattern, ' 192abc ')
If RESULT1:
Print Result1.group ()
Else
print ' Match failed 1 '
RESULT2 = Re.match (pattern, ' abc192 ')
If RESULT2:
Print Result2.group ()
Else
print ' Match failed 2 '
#输出结果--------> 192 match failed 2 resul2 match when encountering a does not match, immediately return none
‘‘‘

‘‘‘
# "3" Re.search (Pattern,string[,flags])
The #match () function matches only from the beginning of the string
#search () scans the entire string lookup match

#将正则表达式编译成pattern对象
Pattern = Re.compile (R ' \d+ ')
#使用re. Search matches the text to get a matching result that will not match when none is returned
RESULT1 = Re.search (pattern, ' abc192def ')
If RESULT1:
Print Result1.group ()
Else
print ' Match failed 1 '
#运行结果--------> 192
‘‘‘

‘‘‘
# "4" Re.split (Pattern,string[,maxsplit])
#按照能够匹配的子串将string分割后返回 List
#maxsplit用于指定最大分割次数, if not specified, all will be split
Pattern = Re.compile (R ' \d+ ')
Print re.split (pattern, ' a1b2c3d4 ')
#运行结果-----------> [' A ', ' B ', ' C ', ' D ', ']
‘‘‘

‘‘‘
# "5" Re.findall (Pattern,string[,flags])
#搜索整个string, returns all substrings that can be matched in the form of a list
Pattern = Re.compile (R ' \d+ ')
Print Re.findall (pattern, ' a1b2c3d4 ')
#运行结果----------> [' 1 ', ' 2 ', ' 3 ', ' 4 ']
‘‘‘

‘‘‘
# "6" Re.finditer (Pattern,string[,flags])
#搜索整个string, returns all match objects that can be matched in the form of an iterator
Pattern = Re.compile (R ' \d+ ')
Matchiter = Re.finditer (pattern, ' a1b2c3d4 ')
For match in Matchiter:
Print Match.group ()
#运行结果------>
# 1
# 2
# 3
# 4
‘‘‘

'
# "7" Re.sub (Pattern,repl,string[,count])
#使用repl替换string中每一个匹配的子串后返回替换后的字符串.
#当repl是一个字符串时, you can use \id or \g<id>, \g<name> reference grouping, but you cannot use number 0.
#当repl是一个方法时, this method should only accept one parameter (the match object) and return a string to replace (the returned string cannot be referenced in a group)
#count用于指定最多替换的次数, replace all
P = When not specified Re.compile (R ' (? p<word1>\w+) (? p<word2>\w+) ') #使用名称引用
s = ' I say, hello world! '
Print p.sub (R ' \g<word2> \g<word1> ', s)
P = re.compile (R ' (\w+) (\w+) ') #使用编号
Print p.sub (R ' \2 \1 '), s)
def func (m):
return M.group (1). Title () + ' + m.group (2). Title ()
Print p.sub (func,s)
#输出结果------>
# Say I, World hello!
# Say I, World hello!
# I Say, Hello world!
'


# "8" Re.subn (Pattern,string[,count])
#返回 (Sub (Repl,string[,count]), number of replacements).
s = ' I say, hello world! '
p = re.compile (R ' (\w+) (\w+) ')
Print p.subn (R ' \2 \1 ', s)
def func (m):
return M.group (1). Title () + "+ m.group (2)." Title ()
Print P.subn (func,s)
#运行结果-------->
# (' Say I, World hello! ', 2)
# (' I Say, Hello world! ', 2)

"Python" Python and regular re are the main methods used to enumerate

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.