Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python regex flags

re.A | re.ASCII			Perform ASCII-only matching instead of full Unicode matching

re.I | re.IGNORECASE	Perform case-insensitive matching

re.M | re.MULTILINE		^ matches the pattern at beginning of the string and each newline’s beginning (
).
               			$ matches pattern at the end of the string and the end of each new line (
)

re.S | re.DOTALL		Without this flag, DOT(.) will match anything except a newline

re.X | re.VERBOSE 		Allow comment in the regex

re.L | re.LOCALE		Perform case-insensitive matching dependent on the current locale. Use only with bytes patterns
Comment

python regex flags

import re

target_str = "Joy lucky number is 75
Tom lucky number is 25"

# find 3-letter word at the start of each newline
# Without re.M or re.MULTILINE flag
result = re.findall(r"^w{3}", target_str)
print(result)  
# Output ['Joy']

# find 2-digit at the end of each newline
# Without re.M or re.MULTILINE flag
result = re.findall(r"d{2}$", target_str)
print(result)
# Output ['25']

# With re.M or re.MULTILINE
# find 3-letter word at the start of each newline
result = re.findall(r"^w{3}", target_str, re.MULTILINE)
print(result)
# Output ['Joy', 'Tom']

# With re.M
# find 2-digit number at the end of each newline
result = re.findall(r"d{2}$", target_str, re.M)
print(result)
# Output ['75', '25']
Comment

PREVIOUS NEXT
Code Example
Python :: python decrease gap between subplot rows 
Python :: open link from python 
Python :: change default python version mac 
Python :: convert string list to float 
Python :: python get day name 
Python :: python calculate time taken 
Python :: create a window turtle python 
Python :: python save seaborn plot 
Python :: pig latin translator python 
Python :: pandas select all columns except one 
Python :: Flask Gmail 
Python :: django flush database 
Python :: who is a pythonista 
Python :: choice random word in python from a text file 
Python :: reverse column order pandas 
Python :: execute command and get output python 
Python :: change size of selenium window 
Python :: numpy install with pip 
Python :: enter key press bind tkinter 
Python :: install python 3.9 ubuntu 
Python :: count unique values numpy 
Python :: counter in django template 
Python :: Convert a Video in python to individual Frames 
Python :: normalize values between 0 and 1 python 
Python :: How to fix snap "pycharm-community" has "install-snap" change in progress 
Python :: python get newest file in directory 
Python :: select closest number in array python 
Python :: random gen in python 
Python :: open website python 
Python :: python copy file 
ADD CONTENT
Topic
Content
Source link
Name
4+4 =