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

How to use regular expressions in Python Grammar Learning

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of "how to use regular expressions in Python grammar learning". The editor shows you the operation process through an actual case. The method of operation is simple, fast and practical. I hope this article "how to use regular expressions in Python grammar learning" can help you solve the problem.

To successfully match strings, you need to use regular expression modules, regular expression matching rules, and strings that need to be matched. In these three conditions, modules and strings are ready, only the matching rules are extraordinarily flexible, and today's chapter is to understand the special characters in regular expressions, through which we can match the data we want.

Special characters in regular expressions special character descriptions\ d match any decimal number, match [0-9] any non-numeric\ w match any alphanumeric underscore and unicode character set\ W match non-alphanumeric data and underscore\ s match any space character The same as [\ n\ t\ r\ v\ f] matches the beginning\ Z end of any non-empty character\ A matching string. Matches any character (except\ n); also called the use of wildcard regular expressions

Let's take a look at a small case to help us understand how to use these special characters.

Import retest_data = "My name is Neo, Ichimm 30 years old." # assign a string of strings to the variable test_dataresult_int = re.findall ('\ dmatch, test_data) # match test_data (match numbers only) result_Space = re.findall ('\ s') using the findall function and pass in the matching rules of'\ d' Test_data) # match test_data using the findall function and passing in'\ d 'matching rules result_str = re.findall ('\ spaces, test_data) # match test_data (matching string) result_str_start = re.findall ('\ AMy'') using the findall function and passing in'\ d 'matching rules Test_data) # matches the string that starts with My result_str_start_null = re.findall ('\ AMya', test_data) # matches the string that starts with Mya (there is no mya Returns an empty list) result_str_end = re.findall ('old.\ Zero, test_data) # the match ends with old. The string result_str_end_null = re.findall ('zold.\ Zero, test_data) # matches ends with zold. String (no zold, empty list returned) result_all = re.findall ('.', test_data) # matches all characters except\ n (including spaces) print (result_int) print (result_Space) print (result_str) # as a result,\ w is more advanced than\ d, not only matching str It also matches int (in fact, the int here is still a string) print (result_str_start) print (result_str_start_null) print (result_str_end) print (result_str_end_null) # > > the execution result is as follows # > ['3percent,' 0'] # > ['',','] # > ['masked,' yearly,'n'' 'Another, 'masked,' oiled, 'old.',' masked, '3percent,' 0percent, 'yawns,' eBay, 'avers,' rants, 'boys,' oysters, 'oysters,' lags,'d'] # > ['My'] # > > [] > [] # > > [' old.'] # > > [] 'yawning,', 'nasty,' await, 'masked,' ethereal,',',', 'smiles,', 'oaths,' lags, 'dudes,'.] Regular small case-1

1. Define a function to determine whether the incoming parameter contains a number.

2. Define a function to determine whether the incoming parameter contains a number, and remove it if so.

Import redef have_number (data): # define a function result = re.findall ('\ dnumbers, data) # use the\ d rule of the findall function of the re module to determine whether the incoming data has a number print (result) for i in result: # use the for loop to determine the result of result, and return True if it exists Instead, return False return True return Falsedef remove_number (data): result = re.findall ('\ ordered, data) print (result) return''.join (result) if _ _ name__ =' _ _ main__': test_data_1 = "My name is Neo, Isim30 year's old." Test_data_2 = "it's a beautiful day to be with you" result = have_number (test_data_1) print (result) result = remove_number (test_data_1) print (result) result = re.findall ('\ numbers, test_data_2) # matches non-alphanumeric data and underscores print (result)

The running result is as follows:

Regular small case-2

1. Define a startwith function to determine whether the incoming data is the beginning of a string.

2. Define an endwith function to determine whether the incoming data is the end of a string.

Import redef startswith (sub, data): _ sub ='\ A {} '.format (sub) result = re.findall (_ sub, data) for i in result: return True return Falsedef endswith (sub, data): _ sub =' {}\ Z'.format (sub) result = re.findall (_ sub) Data) if len (result) = = 0: return False else: return True if _ _ name__ = ='_ _ main__': result = startswith ('My', test_data_1) print (result) result = endswith (' old.', test_data_2) print (result)

The implementation results are as follows:

Regular small case-3

1. The python built-in function len () can get the length of the string, but it will also be calculated in the length when there is a space symbol in the string.

2. Using regular knowledge, define a function to calculate the true length of a string.

Import redef real_len (data): result = re.findall ('\ slam, data) return len (result) if _ _ name__ = ='_ _ main__': test_data_1 = "My name is Neo, Isim30 year's old." Test_data_2 = "it's a beautiful day to be with you" print (len (test_data_2) result = real_len (test_data_2) print (result)

The running results are as follows:

This is the end of the introduction to "how to use regular expressions in Python Grammar Learning". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

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