I can't remember Python regular expressions

Easy notes on how to use

import re
ptn = re.compile(r"hoge+")  #Compiling is faster if you want to reuse it
ptn_with_capture = re.compile(r"(hoge)e*")  #Use parentheses for capture
string = r"hogee_qwerty_hogeeeeee"

#Get the first part to match
first_matched_string = re.search(ptn, string).group()
print(first_matched_string)
# => hogee
#If you attach a capture, group(number)Get only that part
first_matched_string = re.search(ptn_with_capture, string).group(1)
print(first_matched_string)
# => hoge

#Get all matching parts in a list
matched_string_list = re.findall(ptn, string)
print(matched_string_list)
# => ['hogee', 'hogeeeeee']
#If you attach a capture, only the capture part will be acquired
matched_string_list = re.findall(ptn_with_capture, string)
print(matched_string_list)
# => ['hoge', 'hoge']

#Get all matching parts with an iterator
matched_string_iter = re.finditer(ptn, string)
print([ s.group() for s in matched_string_iter])
# => ['hogee', 'hogeeeeee']

#Split the string at the matching part
split_strings = re.split(ptn, string)
print(split_strings)
# => ['', '_qwerty_', '']

#Replace the matching part with another string
replace_with = r"→\1←"  #Use what was captured with a backslash and a number.
substituted_string = re.sub(ptn_with_capture, replace_with, string)
print(substituted_string)
# => →hoge←_qwerty_→hoge←

#Minimum match
minimal_ptn = re.compile(r"h.*?e")  # *Or?、+After the symbol that represents repetition, etc.?The minimum match with.
minimal_matched_string = re.search(minimal_ptn, string)
print(minimal_matched_string.group())
# => hoge

Recommended Posts

I can't remember Python regular expressions
[Python] Regular Expressions Regular Expressions
Use regular expressions in Python
About Python and regular expressions
I can't install python3 with pyenv-vertualenv
I can't install scikit-learn in Python
Handling regular expressions with PHP / Python
When using regular expressions in Python
I can't debug python scripts in Eclipse
Replace non-ASCII with regular expressions in Python
Don't use \ d in Python 3 regular expressions!
How to use regular expressions in Python
Python: Simplified morphological analysis with regular expressions
Why can't I install matplotlib in python! !!
I started python
Pharmaceutical company researchers summarized regular expressions in Python
Python pandas: Search for DataFrame using regular expressions
[Python] Get rid of dating with regular expressions
I can't click the Selenium checkbox Python VBA
I compared the speed of regular expressions in Ruby, Python, and Perl (2013 version)
I can't ask you again (?) Python Knowledge Series -Decorator-
I tried Python> autopep8
python regular expression memo
I can't install Anaconda!
I implemented Python Logging
Makes you think that Python regular expressions are great
I sent regular emails from sendgrid on heroku, on python
Relearn Python (Algorithm I)
Regular expression in Python
Regular expression in Python
I tried Python> decorator
Why I chose Python
I compared Python more-itertools 2.5 → 2.6
I can't move to Python3 easily, so let's consider compatibility 2-3
Extract arbitrary strings using Python regular expressions / Use named groups
Since Python 1.5 of Discord, I can't get a list of members
Get rid of dirty data with Python and regular expressions
Multiple regression expressions in Python
I tried scraping with Python
I wrote python in Japanese
curl -I python one liner
I made blackjack with python!
Python 處 處 regular expression Notes
Use regular expressions in C
I compared Java and Python!
5 reasons I got into Python
Wrap long expressions in python
Extract numbers with regular expressions
I tried Python C extension
[Python] I tried using OpenPose
I made a python text
Regular expression manipulation with Python
I can't search with # google-map. ..
I ran python on windows
I tried gRPC with Python
I tried scraping with python
I understand Python in Japanese!
I made blackjack with Python.
What I learned in Python
I learned Python basic grammar
I made wordcloud with Python.