Regular basis

import re

The #\b metacharacter represents the beginning or end of the word, that is, the boundary of the word does not represent punctuation, space, or newline
# pattern = re.compile(r'\bwe\b')
# s = pattern.findall('we We well welcome')
# print(s)

#Matches everything between we and work using metacharacters
# . matches any character except newline
# * means * the preceding content is repeated any number of times continuously so that the entire expression is expressed
#.* matches any number of characters without newlines
# pattern = re.compile(r'\bwe\b.*\bwork\b')
# s = pattern.findall('we We well welcome like work')
# print(s)

#match all letters starting with s
# pattern = re.compile(r'\bw\w*\b')
# pattern = re.compile(r'w\d*\b')# can match w100 ?????? I want to use ^$ what to do
# s = pattern.findall('we We well w100 selcome like work')
# print(s)

#Escape character metacharacters When ordinary characters are used, they need to be escaped to cancel the special meaning of special characters (metacharacters)
# pattern = re.compile(r'\w*://\w*\.\w*\.\w*/')# can match w100 ?????? I want to use ^$ what to do
# s = pattern.findall('https://www.baidu.com/')
# print(s[0])

# #Repeat + Repeat one or more times
# pattern = re.compile(r'hello\d+')
# s = pattern.findall('hello1 hello100')
# print(s)
#
# # Match strings of 5 to 12 characters
# pattern = re.compile(r'^\d{5,12}$')
# s = pattern.findall('376100870')
# print(s)
#
# #? matches 0 or 1 time
# pattern = re.compile(r'we\d?')
# s = pattern.findall('we we100')
# print(s)

#[] matches any character in []
# pattern = re.compile(r'[.?]')
# s = pattern.findall('we w.e1?00')
# print(s)

#Branch condition| If any rule is satisfied, it should match successfully
#match each condition from left to right and ignore other conditions
# pattern = re.compile(r'0\d{2}-\d{8}|0\d{3}-\d{7}')
# s = pattern.findall('010-11223344 0123-2233445')
# print(s)

#match each condition from left to right and ignore other conditions
# pattern = re.compile(r'\d{3}|\d{8}')
# s = pattern.findall('11223344')
# print(s)

#match IP address 192.168.1.1
pattern = re.compile(r'((\d{1,3})\.){3}\d{1,3}')
s = pattern.search('192.168.1.1')
print(s.group())

Each number in #IP cannot be greater than 255??????
pattern = re.compile(r'((25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)\.){3}(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)')
s = pattern.search('256.168.1.1')
print(s.group())

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324820195&siteId=291194637