Python drill-down regular expression

Source: Internet
Author: User

#!/usr/bin/env Python3
Import re

‘‘‘
#常用功能介绍

[Pp]ython matches "python" or "python"
Rub[ye] matches "Ruby" or "Rube"
[Aeiou] matches any one of the letters within the brackets
[0-9] matches any number. Similar to [0123456789]
[A-z] matches any lowercase letter
[A-z] matches any uppercase letter
[a-za-z0-9] matches any letter and number
[^aeiou] All characters except the Aeiou letter
[^0-9] matches characters except for numbers

. Matches any single character except "\ n". To match any character including ' \ n ', use a pattern like ' [. \ n] '.
\d matches a numeric character. equivalent to [0-9].
\d matches a non-numeric character. equivalent to [^0-9].
\s matches any whitespace character, including spaces, tabs, page breaks, and so on. equivalent to [\f\n\r\t\v].
\s matches any non-whitespace character. equivalent to [^ \f\n\r\t\v].
\w matches any word character that includes an underscore. Equivalent to ' [a-za-z0-9_] '.
\w matches any non-word character. Equivalent to ' [^a-za-z0-9_] '.
‘‘‘



strings = ' 1K3JKSL24KLJZDIO43NDSFOI4NSADF '

#re. Compile (): Complie () compiles objects that need to be matched first, with the benefit of increasing execution speed

Re_complie=re.compile ("[0-9]+")


#match matches from the beginning of the entire object, the returned type is a string
SB = Re_complie.match (strings)
Print (Sb.group ())

#search look in the entire object, find the first return value, the returned type is a string
SB = Re_complie.search (strings)
Print (Sb.group ())

#findall look in the entire object, return a list
SB = Re_complie.findall (strings)
Print (SB)


#re. Split (): Split () User Split object
#split divide the matched format into a list as a split point pair of strings
SB = Re_complie.split (strings)
Print (SB)


#sub replace the characters that match
#count specify how many to replace, for example, 5 elements found in this example, specifying a replacement of four elements
SB = Re_complie.sub ("|", strings,count=4)
Print (SB)


#实例1: Match phone number

Phone_number = ' My name is Andy, my phone number was 13162156638, your call me Andy '
P_number= Re.search ("(1) [3458][0-9]{9}", Phone_number) #方法一
P_number= Re.search ("(1) [3458]\d{9}", Phone_number) #方法二
Print (P_number)


#实例2: Match IP address
IPAddress = "inet 192.168.60.223 netmask 0xffffff00 broadcast 192.168.60.255"
ip = Re.search ("(\d{3}.) {2} (\d{1,3}.) {2} ", IPAddress) #方法一
ip = Re.search ("(\d{1,3}.) {4} ", IPAddress) #方法二
Print (Ip.group ())


#实例3: Grouping Matching

Message = ' Oldboy School, Beijing changping shahe:010-8343245 '
Match = Re.search (R ' (\w+) (\w+) (\s+) ', message)

Print (Match.group ())

#实例3: Match Email address

message = ' [email protected] http://blog.163.com '
emails = "[email protected] http://www.oldboyedu.com"

email = re.search ("[A-za-z0-9][email protected][0-9]+\.com", message)
m = Re.search (r "[0-9.a-z]{0,26}@[0-9.a-z]{0,20}.[ 0-9a-z]{0,8} ", emails)
Print (Email.group ())
Print (M.group ())

Python drill-down 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.