Pharmaceutical company researchers summarized regular expressions in Python

Introduction

Here, we will explain the basics of regular expressions in Python.

Main regular expressions

import re

m1 = re.match(r'ab*', 'a') #Repeat 0 or more times of the previous character (b)
if m1:
    print(m1.group(0))
else:
    print('Not match')

m2 = re.match(r'ab+', 'a') #One or more repetitions of the previous letter (b)
if m2:
    print(m2.group(0))
else:
    print('Not match')

m3 = re.match(r'ab?', 'abb') #Repeat 0 or 1 of the previous character (b)
if m3:
    print(m3.group(0))
else:
    print('Not match')

m4 = re.match(r'ab$', 'abb') #Does it match the end of the string?
if m4:
    print(m4.group(0))
else:
    print('Not match')

m5 = re.match(r'[a-e]', 'f') # []One of the characters in (a, b, c, d,Does it match e)
if m5:
    print(m5.group(0))
else:
    print('Not match')

Regular expression function

import re

#Does it match from the beginning
match = re.match(r'\d+-*\d+$', '012-3456')
print(match.group(0)) # '012-3456'

#Do you match on the way
search = re.search(r'\d{3}', '012-3456')
print(search.group(0)) # '012'

#List all matching patterns
print(re.findall(r'\d{3}', '012-3456')) # ['012', '345']

#Split by the specified pattern delimiter
print(re.split(r'[,、]', '1,2, san')) # ['1', '2', 'Mr.']

#Convert characters of the specified pattern to another pattern
print(re.sub(r'(\d),(\d)', r'\2,\1', '1,2, san')) # 2,1, san

Summary

Here, I explained the basics of regular expressions in Python. If you want to match a specific string pattern, it is convenient to use a regular expression.

Reference materials / links

What is the programming language Python? Can it be used for AI and machine learning?

Recommended Posts

Pharmaceutical company researchers summarized regular expressions in Python
Pharmaceutical company researchers summarized classes in Python
Pharmaceutical company researchers summarized functions in Python
Pharmaceutical company researchers summarized variables in Python
Pharmaceutical company researchers summarized file scanning in Python
Pharmaceutical company researchers summarized Python control statements
Pharmaceutical company researchers summarized Python unit tests
Pharmaceutical company researchers summarized Python exception handling
Pharmaceutical company researchers have summarized the operators used in Python
Pharmaceutical company researchers summarized Python coding standards
Pharmaceutical company researchers summarized web scraping using Python
Pharmaceutical company researchers summarized database operations using Python
Pharmaceutical company researchers summarized SciPy
Pharmaceutical company researchers summarized RDKit
Pharmaceutical company researchers summarized scikit-learn
Use regular expressions in Python
Pharmaceutical company researchers summarized Pandas
Pharmaceutical company researchers summarized NumPy
Pharmaceutical company researchers summarized Matplotlib
Pharmaceutical company researchers summarized Seaborn
Pharmaceutical company researchers summarized Python's comprehensions
When using regular expressions in Python
[Python] Regular Expressions Regular Expressions
Overlapping regular expressions in Python and Java
Pharmaceutical company researchers summarized Python's data structures
Replace non-ASCII with regular expressions in Python
Don't use \ d in Python 3 regular expressions!
How to use regular expressions in Python
How to install Python for pharmaceutical company researchers
Regular expression in Python
Regular expression in Python
Multiple regression expressions in Python
Use regular expressions in C
Wrap long expressions in python
About Python and regular expressions
I can't remember Python regular expressions
Handling regular expressions with PHP / Python
A pharmaceutical company researcher summarized the basic description rules of Python
A memo that handles double-byte double quotes in Python regular expressions
Regular expressions that are easy and solid to learn in Python
difference between statements (statements) and expressions (expressions) in Python
Start / end match in python regular expression
Python: Simplified morphological analysis with regular expressions
I compared the speed of regular expressions in Ruby, Python, and Perl (2013 version)
100 Language Processing Knock Regular Expressions Learned in Chapter 3
Python pandas: Search for DataFrame using regular expressions
[Python] Get rid of dating with regular expressions
Remove extra strings in URLs with regular expressions
Regular expression symbolic group name in Python / Ruby
Quadtree in Python --2
Python in optimization
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Meta-analysis in Python
Unittest in python
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python