search
HomeBackend DevelopmentPython TutorialPython regular expressions

Python regular expressions

Oct 19, 2016 pm 04:37 PM
python basics

Regular expressions are a powerful and standard method for searching, replacing and parsing complex strings. All regular expressions in Python are under the re module.

1 Commonly used matches

^ matches the beginning of the string

$ matches the end of the string

b matches the boundary of a word

d matches any number

D matches any non-numeric character

x? matches one Optional x (matches x character 1 or 0 times)

x* matches x 0 or more times

x+ matches x 1 or more times

x{n,m} at least n times and at most m times x

(a|b|c) either matches a, or matches b, or matches c

(x) generally represents a memory group, you can use the groups() function of the object returned by the re.search function to obtain it Its value

2 General purpose

#-------------------------------------------------------------------------------
# coding:        utf-8
# Purpose:正则表达式
#
# Author:      zdk
#
# Created:     26/02/2013
# Copyright:   (c) zdk 2013
#-------------------------------------------------------------------------------
  
import re
if __name__ == '__main__':
    addr = "100 BROAD ROAD APT.3"
    print(re.sub("ROAD","RD",addr)) # 100 BRD RD APT.3
    print(re.sub(r"\bROAD\b","RD",addr)) # 100 BROAD RD APT.3
    pattern = ".*B.*(ROAD)?"
    print(re.search(pattern,"ROAD")) #None
    print(re.search(pattern,"B")) #<_sre.SRE_Match object at 0x0230F020><span style="background-color:#FAFAFA;font-family:Monaco, &#39;DejaVu Sans Mono&#39;, &#39;Bitstream Vera Sans Mono&#39;, Consolas, &#39;Courier New&#39;, monospace;font-size:1em;line-height:1.5;"> </span>

(1) re.sub("ROAD","RD",addr) Use the re.sub function to search the string addr, and use "RD" to satisfy the expression "ROAD" "Replace

(2) re.sub(r"bROADb","RD",addr), "b" means "word boundary", in Python, because the character "" must be escaped in the string, This can get very cumbersome, so Python prefixes it with r to indicate that all characters in the string are not escaped.

(3) re.search(pattern, "ROAD") The re module has a search function. This function has two parameters, one is a regular expression and the other is a string. The search function returns a search function that can be described in a variety of ways. This matching object returns None if no match is found.

3 Loose regular expressions

The above are all "compact" type expressions, which are more difficult to read. Even if the meaning of the expression is clear now, there is no guarantee that it will be remembered a few months later. Therefore, Python allows users to use so-called loose regular expressions to complete the needs of inline documents. There are two main differences from general expressions in the following two aspects: whitespace characters are ignored. Spaces, tabs, and carriage returns do not match themselves (if you want to match a space in a loose regular expression, you do not need to escape it by adding a backslash before it)

Ignore Note. Like normal Python code, comments start with the # symbol and end at the end of the line.

#松散带有内联注释的正则表达式
    pattern = """
    ^   # begin of string
    M{0,3} # 0 to 3 M
    (CM|CD|D?C{0,3}) #CM or CD or D or D 0 to 3 C
    $   #end of string
    """
    print(re.search(pattern,"MCM",re.VERBOSE)) #<_sre.SRE_Match object at 0x021BAF60>
    print(re.search(pattern,"M99",re.VERBOSE)) #None

(1) When using loose regular expressions, the most important thing is: an extra parameter re.VERBOSE must be passed, which is a constant of the re module, indicating that the regular expression to be matched is a Loose regular expressions. Pattern spaces and comments are ignored, but at the same time have better readability.

4 Case Studies: Parsing phone numbers

must match the following phone numbers:

800-555-1212

800 555 1212

800.555.1212

(800)555-1212

1-8 00-555 -1212

800-555-1212-1234

800-555-1212x1234

800-555-1212 ext.1234

work 1-(800) 555,1212 #1234

Format comparison Much we need to know The area code is 800, the trunk number is 555, and the other digits of the phone number are 1212. For those with extension numbers, we need to know that the extension number is 1234

phonePattern = re.compile(r&#39;&#39;&#39;
    # don&#39;t match beginging of string
(\d{3}) # 3 digits
\D*     #any number of non-digits
(\d{3}) # 3 digits
\D*     #any number of non-digits
(\d{4}) # 4 digits
\D*     #any number of non-digits
(\d*)   #any number of digits
&#39;&#39;&#39;,re.VERBOSE)
print(phonePattern.search(&#39;work 1-(800)555.1212 #1234&#39;).groups()) #(&#39;800&#39;, &#39;555&#39;, &#39;1212&#39;, &#39;1234&#39;)

print(phonePattern.search('work 1-(800)555.1212 # 1234').groups()) #('800', '555', '1212', '1234')

(1) A loose regular expression as above, first match 3 digit area codes (not necessarily from the first It starts with characters, so there is no use ^), then matches any number of non-numeric characters, then matches 3 numeric trunk numbers, then matches any number of non-numeric characters, then matches 4 numeric numbers, and then matches any number of non-numeric characters, then match any number of digit extension numbers, and then use the groups function to group them to get the correct phone number.

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Merging Lists in Python: Choosing the Right MethodMerging Lists in Python: Choosing the Right MethodMay 14, 2025 am 12:11 AM

TomergelistsinPython,youcanusethe operator,extendmethod,listcomprehension,oritertools.chain,eachwithspecificadvantages:1)The operatorissimplebutlessefficientforlargelists;2)extendismemory-efficientbutmodifiestheoriginallist;3)listcomprehensionoffersf

How to concatenate two lists in python 3?How to concatenate two lists in python 3?May 14, 2025 am 12:09 AM

In Python 3, two lists can be connected through a variety of methods: 1) Use operator, which is suitable for small lists, but is inefficient for large lists; 2) Use extend method, which is suitable for large lists, with high memory efficiency, but will modify the original list; 3) Use * operator, which is suitable for merging multiple lists, without modifying the original list; 4) Use itertools.chain, which is suitable for large data sets, with high memory efficiency.

Python concatenate list stringsPython concatenate list stringsMay 14, 2025 am 12:08 AM

Using the join() method is the most efficient way to connect strings from lists in Python. 1) Use the join() method to be efficient and easy to read. 2) The cycle uses operators inefficiently for large lists. 3) The combination of list comprehension and join() is suitable for scenarios that require conversion. 4) The reduce() method is suitable for other types of reductions, but is inefficient for string concatenation. The complete sentence ends.

Python execution, what is that?Python execution, what is that?May 14, 2025 am 12:06 AM

PythonexecutionistheprocessoftransformingPythoncodeintoexecutableinstructions.1)Theinterpreterreadsthecode,convertingitintobytecode,whichthePythonVirtualMachine(PVM)executes.2)TheGlobalInterpreterLock(GIL)managesthreadexecution,potentiallylimitingmul

Python: what are the key featuresPython: what are the key featuresMay 14, 2025 am 12:02 AM

Key features of Python include: 1. The syntax is concise and easy to understand, suitable for beginners; 2. Dynamic type system, improving development speed; 3. Rich standard library, supporting multiple tasks; 4. Strong community and ecosystem, providing extensive support; 5. Interpretation, suitable for scripting and rapid prototyping; 6. Multi-paradigm support, suitable for various programming styles.

Python: compiler or Interpreter?Python: compiler or Interpreter?May 13, 2025 am 12:10 AM

Python is an interpreted language, but it also includes the compilation process. 1) Python code is first compiled into bytecode. 2) Bytecode is interpreted and executed by Python virtual machine. 3) This hybrid mechanism makes Python both flexible and efficient, but not as fast as a fully compiled language.

Python For Loop vs While Loop: When to Use Which?Python For Loop vs While Loop: When to Use Which?May 13, 2025 am 12:07 AM

Useaforloopwheniteratingoverasequenceorforaspecificnumberoftimes;useawhileloopwhencontinuinguntilaconditionismet.Forloopsareidealforknownsequences,whilewhileloopssuitsituationswithundeterminediterations.

Python loops: The most common errorsPython loops: The most common errorsMay 13, 2025 am 12:07 AM

Pythonloopscanleadtoerrorslikeinfiniteloops,modifyinglistsduringiteration,off-by-oneerrors,zero-indexingissues,andnestedloopinefficiencies.Toavoidthese:1)Use'i

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software