8.10.19 learning Summary

Source: Internet
Author: User

Tag: contains the successfully matched span data exchange proc length [] com

1 Regular Expression
Import re
# [] Use-for connection during range matching
# Re. findall ("[a-zA-Z0-9]", "a AB ABC ABCD a123c ")
# If you want to match the symbol-the left or right side of the expression to be written
# Print (Re. findall ("[-AB]", "a AB ABC ABCD a123c --"))

# Number of times of repeated matching expressions
# * Indicates any number of times. Therefore, the value is 0.
Print (Re. findall ("[A-Za-Z] *", "a AB ABC abcdssdsjad a123c "))
# [A-Za-Z] *
# + Once or multiple times
Print (Re. findall ("[A-Za-Z] +", "a AB ABC abcdssdsjad a123c "))
# [A-Za-Z] +
#? 0 or 1 time
Print (Re. findall ("[A-Za-Z]? "," A AB ABC abcdssdsjad a123c "))

# {1, 2} custom match count {1,} 1 to infinity {, 1} 0 to 1
Print (Re. findall ("[A-Za-Z] {1, 2}", "a AB ABC abcdsdssjad a123c "))

 

# + * Obtain as many as possible when the greedy match expression matches (keep matching until it is not satisfied)

# Print (Re. findall ("\ W *", "jjsahdjshdjssadsa dssddsads "))
# Print (Re. findall ("\ W +", "jjsahdjshdjssadsa dssddsads "))
# Add a non-Greedy match after the expression?
# Print (Re. findall ("\ W? "," Jjsahdjshdjssadsa dssddsads ") # non-Greedy match
#


TEXT = " """

# Demonstrate greedy match
Print (Re. findall ('src = "(HTTP .*?) "', Text ))

# Add a non-Greedy match after the expression?
Print (Re. findall ('src = "HTTP. *" ', text ))


# Print (Re. findall ("[A-Za-Z] *", "A | AB | ABC | ABCD | a123c "))

 

# Adding a group to a group does not change the original rule. Only the content in the brackets is taken out separately.
Print (Re. findall ("([A-Za-Z] +) _ DSB", "aigen_dsb cxx_dsb alex_dsb zxx_xsb _ DSB "))


# Common functions in re Module
# Match: Only one matching string is found.
Print (Re. Match ("\ W *", "ABC"). Group (0) # obtain matched content
# The group is used to obtain the content of a group. By default, The 0th group is the entire expression.
Print (Re. Match ("([A-Za-Z] +) (_ DSB)", "aigen_dsb cxx_dsb alex_dsb zxx_xsb _ DSB"). Group (2 ))
Print (Re. Match ("\ W *", "ABC"). span () # obtain the index of the matched content


Print (Re. Search ("\ W *", "ABC"). Group ())
# Obtain one from the full text range
Print (Re. Search ("([A-Za-Z] +) (_ DSB)", "XXX aigen_dsb cxx_dsb alex_dsb zxx_xsb _ DSB "))
# Match from the starting position
# Print (Re. Match ("([A-Za-Z] +) (_ DSB)", "XXX aigen_dsb cxx_dsb alex_dsb zxx_xsb _ DSB"). Group ())
# Compile a regular expression into an object. You do not need to write the expression to start matching.
# Print (Re. Compile ("\ W *"). findall ("ABCD "))


# Print (Re. Split ("\ | _ * \ |", "Python | ____ | JS | ____ | Java "))

 

 

# Replacement
Print (Re. sub ("Python", "Python", "JS | Python | Java "))
# Use regular expressions to exchange positions
TEXT = "Java | C ++ | JS | c | Python"
# Text1 = "Java | C ++ | JS | c | Python"
# Divide the entire content into three parts: Java | C ++ xxxxxx | Python
Partten = "(. + ?) (\ |. + \ |) (. + )"
". +? Ahgshags"
#? : Used to cancel a group. This is the same as that without parentheses.
# Partten = "(? :. + ?) (\ |. + \ |) (. + )"
# Print (Re. Search (partten, text). Group (0 ))
Print (Re. sub (partten, R "\ 2 \ 3 \ 1", text ))


# When the content to be matched contains \
TEXT = "A \ P"
"\ P"

Print (text)
Print (Re. findall (R "A \ P", text ))


# The length of the QQ password is 6-16 digits and special letters do not contain ^
# If ^ does not match any content
# Match all except ^


"[^ \ ^] {6, 16 }"

Import re
# Print (Re. Search ("[^] {6, 16}", "1234567 ^ As ^ "))
# Print (Re. Search ("[^ [\ ^] +. {6, 16}", "1234567as "))

# Print (Re. Match ("[^ @] {6, 16}", "[email protected]")
#
# Print (Re. Match ("[A-Z] {6, 16}", "abasadsasasa ^ "))
# The length must be 6 and cannot contain @
Print (Re. Match ("^ [^] {6, 8} $", "1111111 ^ 56781111 "))

# Print (Re. Match ("[0-9] {6, 7}", "1234567 "))
# Print (Re. Match ("^ \" [^ @] {6, 16} \ "$", '"1234567io1u "'))

# ^ $ Overall matching regards the string content as a whole instead of matching one by one as before
Print (Re. Match ("^ [^] {1234567} $ "))

#

 

 


# Mobile phone number verification length 11 All numbers starting with 1
Print (Re. Match ("^ 1 (89 | 80 | 32) \ D {8} $", "18921999093 "))


# Email address verification letter/digit underline (at least 6) @ letter/digit underline (at least one). (CN com org Edu any one) can have [email protected]
Partten = "^ \ W {6, }@\ W + \. (CN | com | org | EDU) $"
# Only Accept QQ Sina 163

Print (Re. Match (partten, "18925oas [email protected]")


# The ID number must be either 18 or 15 digits. the last digit may be X.
# Partten = "^ \ D {17} (X | \ D) $"
Partten2 = "(^ \ D {15} $) | (^ \ D {17} (X | \ D) $ )"
Print (Re. Match (partten2, "123321200010100 "))

 

2 Import subprocess
# It is used to execute system commands
Import OS

Cmd = r'dir D: \ Shanghai Python full stack 4 \ day23 | findstr "py "'
# Res = subprocess. popen (CMD, shell = true, stdout = subprocess. Pipe, stderr = subprocess. Pipe)
# Reading data from an MPS queue is the medium for communication between two processes.
# Print (type (res. stdout. Read (). Decode ("GBK ")))
# Print (res. stdout. Read (). Decode ("GBK "))
# Print (res. stderr. Read (). Decode ("GBK "))

Dir = r'dir D: \ Shanghai Python full stack 4 \ day23'
Find = 'findstr "py "'
"""
Stdout output Pipeline
Stdin input Pipeline
Stderr error Pipeline
"""
RES1 = subprocess. popen (Dir, shell = true, stdout = subprocess. Pipe, stderr = subprocess. Pipe)

RES2 = subprocess. popen (find, shell = true, stdout = subprocess. Pipe, stderr = subprocess. Pipe, stdin = res1.stdout)
# Reading data from an MPS queue is the medium for communication between two processes.
# Print (type (res. stdout. Read (). Decode ("GBK ")))
# Print (res1.stdout. Read (). Decode ("GBK "))
Print (res2.stderr. Read (). Decode ("GBK"), "33333 ")

 

# In brief, subprocess is mainly used to execute system commands (promoter processes). The difference between subprocess and OS. system is that
# Subprocess can exchange data with this sub-process

 

8.10.19 learning Summary

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.