Features of regular expression modules that I often use personally in Python

I often forget, but the Official Document has too much text and it's hard to check it every time, so it's a memorandum. ,. We plan to add more as needed.

import re

By the way, note that from sympy import * will result in a re function that returns the real part.

Match object

To take out the matched part ...

filename = 'Back_in_the_U.S.S.R.m4a'
m = re.match(r'([\w\.-]+?)\.(\w+)$', filename)
print(m.group(0)) #The whole match(m.group()Equivalent to?)
print(m.group(1)) #1st group
print(m.group(2)) #Second group
print(m.groups()) #Tuple the entire group

out


Back_in_the_U.S.S.R.m4a
Back_in_the_U.S.S.R
m4a
('Back_in_the_U.S.S.R', 'm4a')

Name the group with (? P <groupname>) and access from the keyword.

filename = 'Back_in_the_U.S.S.R.m4a'
m = re.match(r'(?P<basename>[\w\.]+?)\.(?P<ext>\w+)$', filename)
print(m.group('basename')) #(?P<basename> )String that matches
print(m.group('ext')) #(?P<ext> )String that matches
print(m.groupdict()) #Named groups in a dictionary

out


Back_in_the_U.S.S.R
m4a
{'basename': 'Back_in_the_U.S.S.R', 'ext': 'm4a'}

Use the matched string for replacement

For example, to replace \ ruby {reductio} {reductio} in $ \ LaTeX $ with <ruby> reductio <rt> reductio </ rt> </ ruby> ...

print(re.sub(r'\\ruby\{(\w+)\}\{(\w+)\}',
             r'<ruby>\1<rt>\2</rt></ruby>',
             r'\ruby{Reductio ad absurdum}{View Plaza}'))

out


<ruby>Reductio ad absurdum<rt>View Plaza</rt></ruby>

Use a group named with (? P <groupname>) Use\ g <groupname>to refer to it.

print(re.sub(r'\\ruby\{(?P<rb>\w+)\}\{(?P<rt>\w+)\}',
             r'<ruby>\g<rb><rt>\g<rt></rt></ruby>',
             r'\ruby{Reductio ad absurdum}{View Plaza}'))

out


<ruby>Reductio ad absurdum<rt>View Plaza</rt></ruby>

It's confusing with the html tag, but I got the same result.

List all matched parts

To retrieve all the contents of an HTML ʻem element or strong` element ...

src = r'<em>Axiom of choice</em>Assuming, to any set<strong>You can put the order</strong>.'
re.findall(r'<(em|strong)>(.*?)</\1>', src)

out


[('em', 'Axiom of choice'), ('strong', 'You can put the order')]

Replace by passing the matched part to the function

To somehow change cm to m in the text ...

def cm2m(m): #Prepare a function that takes a match object as an argument
    value = m.group(1)
    return str(float(value)/100) + 'm'
print(re.sub(r'(\d+)cm', cm2m, '271cm +314 cm is 585 cm.'))

out


2.71m + 3.14m is 5.It is 85m.

I wonder if lambda is good for simple processing that does not require the purpose of defining a function.

print(re.sub(r'(\d+)cm', lambda m: str(float(m.group(1))/100) + 'm', '271cm +314 cm is 585 cm.'))

out


2.71m + 3.14m is 5.It is 85m.

Nested parentheses

Use recursion. However, since it is a function that the standard re does not have, use regex. If it is not installed, use pip install regex etc. The following matches \ frac {} {} in $ \ LaTeX $ (maybe).

import regex
pattern_frac = r'\\frac(?<rec>\{(?:[^{}]+|(?&rec))*\}){2}'
m = regex.search(pattern_frac, r'1 + \frac{\int_{a}^{b} f(x)\,dx }{\sum_{k=1}^{n}a_{k}}')
print(m.group())

out


\frac{\int_{a}^{b} f(x)\,dx }{\sum_{k=1}^{n}a_{k}}

Recommended Posts

Features of regular expression modules that I often use personally in Python
Regular expression in Python
Regular expression in Python
Use let expression in Python
Use regular expressions in Python
A template that I often use when making Discord BOT in Python (memorial note)
I tried to make a regular expression of "amount" using Python
I tried to make a regular expression of "time" using Python
I tried to make a regular expression of "date" using Python
Use print in a Python2 lambda expression
Start / end match in python regular expression
How to use regular expressions in Python
A memo that I wrote a quicksort in Python
[Complete memorandum] A collection of codes that I often use but cannot remember
I want to use Python in the environment of pyenv + pipenv on Windows 10
Talking about the features that pandas and I were in charge of in the project
I compared the speed of regular expressions in Ruby, Python, and Perl (2013 version)
Summary of how to use MNIST in Python
Use various rabbimq features with pika in python
I touched some of the new features of Python 3.8 ①
Regular expression symbolic group name in Python / Ruby
[Question] What happens when I use% in python?
Basics of I / O screen using tkinter in python3
I tried to summarize how to use matplotlib of python
A collection of code often used in personal Python
Dynamically import / reload modules that have changed in Python
Use Cursur that closes automatically with sqlite3 in Python
One liner that outputs 1000000 digits of pi in Python
Let's use the open data of "Mamebus" in Python
I tried to summarize how to use pandas in python
Modules that may go through the shell in Python
A collection of Excel operations often used in Python
A python regular expression, or a memo of a match object
I want to use the R dataset in python
List of python modules
Regular expression in regex.h
Use config.ini in Python
Use dates in Python
Use Valgrind in Python
python regular expression memo
Use profiler in Python
python> link> Mid-line comment in Python?> I was told that it's better to use named arguments.
I tried the accuracy of three Stirling's approximations in python
Processing of python3 that seems to be usable in paiza
python Condition extraction from a list that I often forget
Python --Find out number of groups in the regex expression
I tried "a program that removes duplicate statements in Python"
I measured various methods of interprocess communication in multiprocessing of python3
Comparison of how to use higher-order functions in Python 2 and 3
I want to know the features of Python and pip
A memorandum that you will often use in Python's Selenium
Reproduce the Python regular expression r'\ w (? U)' in JavaScript
A set of script files that do wordcloud in Python3
I tried to implement blackjack of card game in Python
[Python3] List of sites that I referred to when I started Python
Let's use def in python
Use Measurement Protocol in Python
Use callback function in Python
Use parameter store in Python
Use HTTP cache in Python
Python 處 處 regular expression Notes