Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the knowledge points for getting started with Python rules

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Today Xiaobian to share with you Python regular entry knowledge points what are the relevant knowledge points, detailed content, clear logic, I believe most people still know too much about this knowledge, so share this article for everyone to refer to, I hope you read this article after some gains, let's learn about it together.

1. Single character matching

1. Match a string:

text = "abcdef"

ret = re.match('a',text)

print(ret.group())

2. Point (.): Matches any character (except '\n'):

text = "\nabcdef"

ret = re.match('. ',text)

print(ret.group())

3.\d: Match any number:

text = "abcdef"

ret = re.match('\d',text)

print(ret.group())

4.\D: Match any non-numeric:

text = "cabedf"

ret = re.match('\D',text)

print(ret.group())

5.\s: matching white space characters (including: \n,\t,\r and spaces):

text = "\nabdef"

ret = re.match('\s',text)

print("*"*30)

print(ret.group())

print("*"*30)

6.\S: Non-white space characters:

text = "ababdef"

ret = re.match('\S',text)

print("*"*30)

print(ret.group())

print("*"*30)

7.\w: matches a-z and A-Z and numbers and underscores:

text = "1bc"

ret = re.match('\w',text)

print("*"*30)

print(ret.group())

print("*"*30)

8.\W: Match is and\w is opposite:

text = "+bc"

ret = re.match('\W',text)

print("*"*30)

print(ret.group())

print("*"*30)

9,[] combination mode, as long as one of the items in brackets is satisfied, the match is successful:

text = "cba"

ret = re.match('[1c]',text)

print("*"*30)

print(ret.group())

print("*"*30)

10. Use combination method [0-9] \d:

text = "abc"

ret = re.match ('[^0-9]',text) # ^Symbol indicates non

print("="*30)

print(ret.group())

print("="*30)

11. Use combination to realize\w:

text = "+bc"

ret = re.match('[^a-zA-Z0-9_]',text)

print("="*30)

print(ret.group())

print("="*30)

Second, multi-character matching

1. *: Match 0 or more characters:

text = "-cba"

result = re.match('\D*',text)

print(result.group())

2,+: matches one or more characters:

text = "1cba"

result = re.match('\w+',text)

print(result.group())

3、?:Match the previous character 0 or 1:

text = "-cba"

result = re.match('\w? ',text)

print(result.group())

4.{m}: Match m characters:

text = "+1cba"

result = re.match('\w{2}',text)

print(result.group())

5.{m,n}: characters matching the number between m and n:

text = "1cba+"

result = re.match('\w{1,3}',text)

print(result.group())

3. Regular expression cases

1. Verify the mobile phone number: The rule of mobile phone number is to start with 1, the second digit can be 34587, and the last 9 digits can be any number.

text = "17751632549"

result = re.match("1[34587]\d{9}",text)

print(result.group())

2. Verify mailbox: The rule of mailbox is that mailbox name is composed of numbers, English characters, underscores, then @ symbol, then domain name.

text = "w3cschool@163.com"

result = re.match("\w+@[a-z0-9]+\. [a-z]+",text)

print(result.group())

3. Verify URL: The rule of URL is http or https or ftp followed by a colon, plus two slashes, and then any non-white space characters can appear.

text = "https://www.yisu.com/minicourse/play/quick_scrapy"

result = re.match("(http|https|ftp)://\S+",text)

print(result.group())

4. Verification ID card: The rule of ID card is that there are 18 digits in total, the first 17 digits are numbers, and the last digit can be a number, a lowercase x, or a capital X.

text = "35215669985213654x"

result = re.match("\d{17}[\dxX]",text)

print(result.group())

IV. Start/End/Greed and Non-Greed

1,^: by... Beginning:

text = "hello world"

result = re.search("^world",text)

print(result.group())

2,$: with... Ending:

text = "hello world"

result = re.search("hello$",text)

print(result.group())

text = ""

result = re.search("^$",text)

print(result.group())

3、|: Matches multiple strings or expressions:

text = "https://www.yisu.com/minicourse/play/quick_scrapy"

result = re.match("(http|https|ftp)://\S+",text)

print(result.group())

Greed and non-greed:

text = "12345"

result = re.search("\d+? ",text)

print(result.group())

Case 1: Extract html tag name:

text = "This is a first-class title"

result = re.search("",text)

print(result.group())

Case 2: Verify that a character is a number between 0 and 100:

text = "100"

result = re.match("0$|[1-9]\d?$| 100$",text)

print(result.group())

IV. Escape characters and native strings

Escape characters in Python:

text = r"hello\nw3cschool"

print(text)

2. Escape characters in regular expressions:

text = "apple price is $9.9,range price is $8.8"

result = re.findall("\$\d+",text)

print(result)

Native strings and regular expressions:

String parsing rules for regular expressions:

Let's parse this string at the Python language level.

Parse the results of Python language-level parsing at the regular expression level.

text = "\cba c"

result = re.match("\\\\c",text) # \\\c =(Python language level)> \\c =(regular expression level)> \c

result = re.match(r"\\c",text) # \\c =(regular expression level)> \c

print(result.group())

V. Grouping

text = "apple price is $9.9,orange price is $8.8"

result = re.search('.+ (\$\d+).+ (\$\d+)',text)

print(result.groups())

group()/group(0): matches the entire group

group(1): matches the first group

group(2): matches the second group

groups(): Get all the groups

6. Common functions in re

1. findall: Find all the conditions that meet

text = "apple price is $9.9,orange price is $8.8"

result = re.findall(r'\$\d+',text)

print(result)

2, sub: replace other strings according to the rules

text = "nihao zhongguo,hello w3cschool"

new_text = text.replace(" ","\n")

new_text = re.sub(r' |,','\n',text)

print(new_text)

html = """

1. More than 3 years related development experience, full-time recruitment bachelor degree or above

2. Proficient in one or more development languages (Python,C,Java, etc.), at least one of which has more than 3 years of experience

3. Skilled use of ES/mysql/mongodb/redis database;

4. Proficient in using web frameworks such as django and tornado, with independent Python/Java back-end development experience;

5. Familiar with Linux / Unix operating systems

6. Familiar with TCP/IP, http and other network protocols

Benefits:

1. Purchase six insurances and one gold for entry (first-class medical treatment + full purchase of commercial insurance by the company)+ opening + full year-end bonus (13 salary for one year, generally higher than one month)

2. There are 2 salary adjustment opportunities after one year of employment

3. The project is stable, the team stability is high, and the team atmosphere is very good (the proportion of merged employees accounts for nearly 50% of the total employees of China Merchants Bank);

4. Have the opportunity to become an internal employee of China Merchants Bank;

5. The team has its own activity funds every month, and statutory holidays are arranged;

6. Excellent office environment, overtime pay (full salary is the calculation base, overtime does not exceed 10 p.m., weekday overtime pay 1.5 times, weekend overtime pay 2 times, weekend overtime can also give priority to adjustment, humanized management).

"""

new_html = re.sub(r'',"",html)

print(new_html)

Split: Split a string according to rules

text = "nihao zhongguo,hello world"

result = re.split(r' |,',text)

print(result)

4. compile: compile regular expressions

text = "apple price is 34.56"

r = re.compile(r"""

\d+ #integer part

\.? #decimal point

\d* #fractional part

""",re.VERBOSE)

result = re.search(r,text)

result = re.search(r"""

\d+ #integer part

\.? #decimal point

\d* #fractional part

""",text,re.VERBOSE)

print(result.group())

If you want to add comments to a regular expression, you need to add a `re.VERBOSE` at the end of the regular expression function.

The above is all the contents of this article, thank you for reading! I believe everyone has a great harvest after reading this article. Xiaobian will update different knowledge for everyone every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report