Python regex related snippets

PHOTO EMBED

Tue Jul 20 2021 04:01:34 GMT+0000 (Coordinated Universal Time)

Saved by @QuinnFox12 #python #pandas #regex

https://vimsky.com/zh-tw/examples/detail/python-method-regex.sub.html

# Match text between two strings with regular expression
clean_text = re.search(r'before(.*?)after', text).group(1)
import re
s = 'Part 1. Part 2. Part 3 then more text'
re.search(r'Part 1\.(.*?)Part 3', s).group(1)
' Part 2. '
re.search(r'Part 1(.*?)Part 3', s).group(1)
'. Part 2. '
content_copyCOPY

https://stackoverflow.com/questions/32680030/match-text-between-two-strings-with-regular-expression