Using a “re” module findall() function with regex can extract a substring from a given string in Python.
Example extract pattern matches using RegEx in Python
import re text = "Chennai is a beautiful city.Chennai is the capital of the state of Tamil Nadu" city = re.findall("Chennai", text) print(city)
Using the search method in this example with bit complex RegEx.
import re line = '##ParameterValue[part I care about]=garbagegarbage' m = re.search('\[(.*)\]', line) print(m.group(1))
Output: part I care about
Do comment if you have any doubts and suggestions on this Python regular expression tutorial.
Note: IDE: PyCharm 2021.1.3 (Community Edition)
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.
Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.