Python Regular expression Re/re learning notes and simple exercises

Source: Internet
Author: User

#., \w,\s,\d,,^,$
# *,+,?, {n},{n,},{n,m}

The # RE module is used for the operation of a Python regular expression.
#
Characters
#
#   . Match any character other than line break
# \w matches letters or numbers or underscores or kanji
# \s matches any whitespace character
# \d Matching numbers
# \b Matches the beginning or end of a word
# ^ matches the start of the string
# $ match End of string
#
Views
#
# * 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


Import re
# Match (pattern, string, flags=0)
# match the specified content from the starting position according to the model to the string to match the individual
# Regular Expressions
# to match the string
# flag bit, used to control how regular expressions are matched
# strings = Re.match (' \d+ ', ' 123df45 ')
# Print (strings)
# Print (Strings.group ())
# Print (strings.groups ())
# <_sre. Sre_match object; Span= (0, 3), match= ' 123 ' >
# 123
# ()



#根据模型去字符串中匹配指定内容, matching a single
# obj = Re.search (' \d+ ', ' u9f34k ')
# print (obj)
# Print (Obj.group ())
# <_sre. Sre_match object; Span= (1, 2), match= ' 9 ' >
# 9




# Group and Groups
# a = ' 123abc456 '
# Print (Re.search ("([0-9]*) ([a-z]*] ([0-9]*)", a). Group ())
# # 123abc456
# Print (Re.search ("([0-9]*) ([a-z]*) ([0-9]*)", a). Groups ())
# # (' 123 ', ' abc ', ' 456 ')
# Print (Re.search ("([0-9]*) ([a-z]*) ([0-9]*)", a). Group (0))
# # 123abc456
# Print (Re.search ("([0-9]*) ([a-z]*) ([0-9]*)", a). Group (1))
# # 123
# Print (Re.search ("([0-9]*) ([a-z]*) ([0-9]*)", a). Group (2))
# #abc
# Print (Re.search ("([0-9]*) ([a-z]*) ([0-9]*)", a). Group (3))
# #456


# FindAll (pattern, string, flags=0)
# Both of these methods are used to match single values, that is, only one of the strings can be matched, and if you want to match all eligible elements in a string, you need to use FindAll.
# a = ' 123abc567 '
# obj = Re.findall (' \d+ ', a)
# print (obj)
# print ()
# [' 123 ', ' 567 ']


# sub (pattern, REPL, String, count=0, flags=0)
# used to replace matching strings

# content = ' 123abc456 '
# new_content = re.sub (' \d+ ', ' SB ', content)
# # SBABCSB
# Print (new_content)
# new = Re.sub (' \d+ ', ' Xxoo ', content,3)
# Print (new)
# Xxooabcxxoo

Content = "' 1-2 * ((60-30+1* (9-2*5/3+7/3*99/4*2998+10*568/14))-( -4*3)/(16-3*2)) '"
# new_content = Re.split (' \* ', content)
# Print (new_content)
# # [' ' 1-2 ', ' ((60-30+1 ', ' (9-2 ', ' 5/3+7/3 ', ' 99/4 ', ' 2998+10 ', ' 568/14 ') ')-(-4 ', ' 3 ')/(16-3 ', ' 2 ') ' "]
# New_content2 = Re.split (' \* ', content,1)
# Print (New_content2)
# # ["' 1-2", "((60-30+1* (9-2*5/3+7/3*99/4*2998+10*568/14)-( -4*3)/(16-3*2)) ']
#
# New_content3 = Re.split (' \* ', content,3)
# Print (NEW_CONTENT3)
# # [' ' 1-2 ', ' ((60-30+1 ', ' (9-2 ', ' 5/3+7/3*99/4*2998+10*568/14 ')-( -4*3)/(16-3*2)) ']



# new_content = Re.split (' [\+\-\*\/]+ ', content)
# Print (new_content)
# [' ' 1 ', ' 2 ', ' ((60 ', ' 30 ', ' 1 ', ' (9 '), ' 2 ', ' 5 ', ' 3 ', ' 7 ', ' 3 ', ' 99 ', ' 4 ', ' 2998 ', ' 10 ', ' 568 ', ' 14 ') ', ' (', ' 4 ', ' 3 ') ' , ' (16 ', ' 3 ', ' 2) ')


# INPP = ' 1-2* ((60-30 + (-40-5) * (9-2*5/3 + 7/3*99/4*2998 +10 * 568/14))-( -4*3)/(16-3*2)) '
# INPP = re.sub (' \s* ', ' ', INPP)
# Print (INPP)
# # 1-2* ((60-30+ (-40-5) * (9-2*5/3+7/3*99/4*2998+10*568/14))-( -4*3)/(16-3*2))
#
# new_content = Re.split (' \ (([\+\-\*\/]?\d+[\+\-\*\/]?\d+) {1}\) ', INPP, 1)
# Print (new_content)

Python Regular expression Re/re learning notes and simple exercises

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.