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 skills of Python regular expression

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the knowledge about "Python regular expression skills". In the actual case operation process, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!

When writing programs or web pages that handle strings, there is often a need to find strings that conform to some complex rule (or pattern). Regular expressions are tools for describing these rules (or patterns). In other words, regular expressions are code that records rules of text.

Once you find the right text, you can modify it accordingly. Remember the wildcard characters used to find files on the Windows command line, * and?. When we are looking for all PDF documents in a directory, we simply search *.pdf.

Here, * is interpreted as an arbitrary string. Like wildcards, regular expressions are also tools for text matching, but they describe your needs more precisely than wildcards, such as finding all phone numbers in a web page.

We know that telephone numbers generally have a fixed script: area code-telephone number, which starts with 0 followed by 2-3 digits followed by a hyphen "-," and *** is a string of 7 or 8 digits (e.g. 010-12345678 or 0634-1234567).

The simplest regular expression

The *** method of learning regular expressions is to start with specific examples and let readers experiment with them. A number of simple examples are given below and explained in detail. When we look for to in a string, you can use the regular expression to. This is almost the simplest regular expression.

It matches exactly strings consisting of two characters, the first one t and the second one o. To demonstrate, we provide a function re_show(), which can be thought of as a wrapper for the re module, that matches (says a string matches a regular expression) in a given string.

The string is a string of characters that satisfy some or all of the conditions given in the expression. We will not go into this function any further. All you need to know is that *** arguments to re_show() are a Python expression, the second argument is the string to match, and when a match is found, enclose it in curly braces. The source code is as follows:

class Category(models.Model): id = models.AutoField('id', primary_key=True) name = models.CharField(maxlength=50) code = models.CharField(maxlength=50) parentCategory = models.ForeignKey('self', 'id', null=True) enable = models.BooleanField() def __str__(self): return self.name class Admin: list_display = ('id', 'name', 'code', 'parentCategory')

The function call re_show("to",s) is used to find out whether the string s contains the string to, or whether the string s matches the regular expression to; if so, the string to is braced. The result of executing the above code.

"Python regular expression skills what" content is introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!

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