python regular expression memo

Introduction

import re

Raw string

Prefixing a string literal with r disables the escape sequence for that string. (Something like @ "~" in C #)

"C:¥¥My Document¥¥fire¥¥test.txt"

To

r"C:¥My Document¥fire¥test.txt"

Can be written.

It seems that r may be capitalized.

R"C:¥My Document¥fire¥test.txt"

Match

import re

m = re.match(r"FIRE[0-9]+", "FIRE123")

if m:
	print("matched")
else:
	print("Not matched")

Besides the match method, there is also a search method. The match method looks at whether it matches first, and the search method looks at whether it matches in the middle of the string.

m = re.match(r"FIRE[0-9]+", "Python FIRE123")     #Do not match
m = re.search(r"FIRE[0-9]+", "Python FIRE123")    #match

Get the matched string

m = re.search(r"([a-z]+)\s*=\s*([0-9]+)", "index = 123")

if m:
	print(m.group(0))                # ->  "index = 123"
	print(m.group(1))                # ->  "index"
	print(m.group(2))                # ->  "123"

The named group is (? P ...)

m = re.search(r"(?P<name>[a-z]+)\s*=\s*(?P<value>[0-9]+)", "index = 123")

if m:
	print(m.group(0))                # ->  "index = 123"
	print(m.group(1))                # ->  "index"
	print(m.group(2))                # ->  "123"
	print(m.group('name'))           # ->  "index"
	print(m.group('value'))          # ->  "123"

Get the matched position

m = re.match(r"abc([0-9]+).([0-9]+)", "abc123.456")

#---The whole match
print(m.start())                # -> 0
print(m.end())                  # ->10 * end is the next matched position (index)+The value of 1) is returned
print(m.span())                 # -> (0, 10)Returns start and end as tuples

#---Group 1
print(m.start(1))               # -> 3
print(m.end(1))                 # -> 6
print(m.span(1))                # -> (3, 6)


#---Group 2
print(m.start(2))               # -> 7
print(m.end(2))                 # -> 10
print(m.span(2))                # -> (7, 10)

Compile regular expressions

reg = re.compile(r"ABC([0-9]+)")

m = reg.match("ABC888")
print(m.group(1))               # -> "888"

Returns all unique matches as a string list

match_list = re.findall(r'([0-9]+)', "123a456b789")
print(match_list)               # -> ['123', '456', '789']

Recommended Posts

python regular expression memo
Regular expression in Python
Regular expression in Python
Python memo
python memo
Python 處 處 regular expression Notes
Python memo
python memo
Python memo
Regular expression manipulation with Python
Python memo
Python memo
A python regular expression, or a memo of a match object
String replacement with Python regular expression
python beginner memo (9.2-10)
Regular expression Greedy
python beginner memo (9.1)
★ Memo ★ Python Iroha
[Python] Regular Expressions Regular Expressions
[Python] EDA memo
Python 3 operator memo
lambda expression memo
[My memo] python
Python3 metaclass memo
[Python] Basemap memo
Recursive expression memo
Python beginner memo (2)
Regular expression re
[Python] Numpy memo
(Python) HTML reading and regular expression notes
Python class (Python learning memo ⑦)
My python environment memo
Regular expression in regex.h
python openCV installation (memo)
Python module (Python learning memo ④)
Visualization memo by Python
A python lambda expression ...
Python test package memo
[Python] Memo about functions
Regular expression with pymongo
Date notation regular expression
Binary search (python2.7) memo
Regular expression matching method
[My memo] python -v / python -V
Python3 List / dictionary memo
[Memo] Python3 list sort
Python Tips (my memo)
[Python] Memo about errors
DynamoDB Script Memo (Python)
Python basic memo --Part 2
python recipe book Memo
Basic Python command memo
Python OpenCV tutorial memo
Python basic grammar memo
TensorFlow API memo (Python)
Regular expression symbolic group name in Python / Ruby
python useful memo links
Python decorator operation memo
Python basic memo --Part 1
Effective Python Memo Item 3
Regular expression confirmation quiz!