Python regular expression (easy to understand)

catalogue

Hello O(∩ ∩) O??

What is a regular expression (⊙⊙)

In short, regular expressions are

How to use regular expressions?

Usage of sreach??

Match multiple consecutive values??

? The character "+" repeats the previous matching character one or more times??

The character "*" repeats the previous matching character zero or more times??

Character "?" Repeat the previous matching character zero times or once??

Special characters use the backslash "" to guide, such as "", "", "", "", and "" to represent carriage return, line feed, tabulation symbols, and the backslash itself

?

? Complete table??

? match usage??

match usage??

? match object??

Quantifier??

Match the beginning and end??

?

? Match group??

match summary??

Practice makes true knowledge convex ('0 ') convex??

Write at the end??

Hello O(∩ ∩) O??

Today, let's post python regular expressions. In fact, this is relatively simple

It took several hours to write it out

What is a regular expression (⊙⊙)

At present, more and more websites, editors and programming languages have supported a string search "formula" called "Regular Expression". Students with programming experience should understand what Regular Expression (regex) is. It is a pattern of character string matching, which is more like a logical formula.

In short, regular expressions are

The necessary tools in python are mainly used to find and match strings.

Regular expressions are especially used on python crawlers.

How to use regular expressions

First, we need to import the header file (I'm used to writing c++) module

import re

Because re is a built-in module, no additional installation is required, which is very convenient

Usage of sreach??

Match multiple consecutive values??

import re

r=r"d+"
m=re.search(r,"YRYR567eruwgf")#The goal is 567
print(m)

In the re module, r "d+" regular expression means to match multiple consecutive values, and search is a function in re. Search for consecutive values from the "YRYR567eruwgf" string to get "567"

result:

It can be seen that the continuous value "567" is searched

The character "+" repeats the previous matching character one or more times??

import re

r=r"bd+"
m=re.search(r,"a12b1233cd")
print(m)

In this way, the result is the continuous number after b

result:

The character "*" repeats the previous matching character zero or more times??

"*" is similar to "+" but has differences, such as:
It can be seen that r"ab+" matches "ab", but r"ab" matches "a", because it means that "b" can be repeated zero times, but "+" requires "b" to be repeated more than once

import re

r=r"ab+"
m=re.search(r,"acabc")
print(m)

r=r"ab*"
m=re.search(r,"acabc")
print(m)

result:

The character "" repeats the previous matching character zero times or once??

Match the result "ab", repeat b once

import re

r=r"ab?"
m=re.search(r,"abbcabc")
print(m)

result:

Special characters use the backslash "" to guide, such as "", "", "", "", and "" to represent carriage return, line feed, tabulation symbols, and the backslash itself

import re

r=r"a
b"
m=re.search(r,"ca
bcaba")
print(m)

result:

Complete table??

In fact, these are relatively basic, relatively simple, and a little more complex, which are all in this table

match usage??

match usage??

Syntax: re match(pattern, string[, flags])

The matching starts from the first letter. If the string contains a substring of pattern, the matching is successful, and the Match object is returned. If it fails, it returns None. To Match exactly, the pattern must end with $.

#Example:
name='Zhang San'
if re.match('Zhang w+',name):
    print('{},Hello!'.format(name))
    # Hello, Zhang San!

Output result: Hello, Zhang San

Zhang San: who called me?

No kidding, go on

In general, match is

  • Return the matching match object
  • By default, the matching starts from the beginning of the given string, even if the regular expression does not declare the matching beginning with ^

match object??

Several properties of Match object:

Note that there is a "." in front
1..string text to match
2..re match the pattern object used
3..pos regular expression search text start position
4..endpos regular expression search text end position
Several methods of Match object:
1.group(0) returns the matching substring
2.start() returns the start position of the matching substring
3.end() returns the end position of the matching substring
4.span() returns start(), end()

Quantifier??

Match the beginning and end??

Match group??

Well, after looking at the above tables, I think the most important thing is below

match summary??

In fact, there's nothing to summarize, but it's important for you to understand this picture

What I frame is what I feel I often use

In fact, I didn't understand it when I first learned it. Now I think it's simple

So, you should feel very simple now?

However, there are many characters in regular expressions, which are easy to be confused. If you are not careful, dozens of errors will be reported, which is very crashing

Practice produces truth convex ('0 ') convex

After learning so much, do you want to play a program?

It's ready for you

Program effect: enter the mobile number, and judge whether the mobile number is legal through regular expression,

If it is legal, output the information of this mobile number (location, etc.)

If it's illegal, just re-enter it. Is it simple?

Here I want to focus on how to get the information of mobile phone number

At first, I planned to go online for a wave of Baidu, but I didn't expect to copy 23 errors directly. I was also drunk

Oh, no more facial expressions

ε= (′ ο`*))) Alas, I'd better write it myself

I remembered a module called phone, which can realize this function

However, you may not have installed this module yet. Enter pip install phone in the command line mode

It will be downloaded in 6649 seconds

Then you can experience it

Code (PyCharm runs through)

import phone
from time import *
import re

def begin():
    print("Welcome to the inquiry applet")
    print("1.query")
    print("2.user")


def p(n):
    if re.match(r'1[3,4,5,7,8]d{9}', n):
        if re.match(r'13[0,1,2]d{8}', n) or 
            re.match(r"15[5,6]d{8}", n) or 
            re.match(r"18[5,6]", n) or 
            re.match(r"145d{8}", n) or 
            re.match(r"176d{8}", n):
            return True
        elif re.match(r"13[4,5,6,7,8,9]d{8}", n) or 
            re.match(r"147d{8}|178d{8}", n) or 
            re.match(r"15[0,1,2,7,8,9]d{8}", n) or 
            re.match(r"18[2,3,4,7,8]d{8}", n):
            return True
        else:
            return True
    else:
        return False

if __name__ == "__main__":
    s=0
    begin()
    while True:
        op = int(input("Please enter:"))
        if op==1:
            phoneNum = str(input("Please enter your phone number"))
            if p(phoneNum)==False:
                print("The phone number is invalid")
                for i in range(100):
                    print('
')
                begin()
            else:
                info = phone.Phone().find(phoneNum)
                print("phone number:"+str(info["phone"]))
                print("Location of mobile phone:"+str(info["province"])+"province"+str(info["city"])+"city")
                print("Postal code:"+str(info["zip_code"]))
                print("Area number:"+str(info["area_code"]))
                print("Phone type:"+str(info["phone_type"]))
                s+=1
                i = input("Enter any number to exit...")
                for i in range(100):
                    print('
')
                begin()
        if op==2:
            print("Usage times:"+str(s))
            i = input("Enter any number to exit...")
            for i in range(100):
                print('
')
            begin()

Write at the end??

It seems that this blog is quite long. You can see that there are more than 60% people here. If anyone doesn't understand it very well, or has questions about c++ and python, you can send me a private message. I will reply one by one after I see it

In addition, mutual fans must return??

Thank you for reading, bye!

Tags: Back-end Front-end Android Interview

Posted by johanafm on Sat, 30 Jul 2022 01:57:06 +0930