Ads by ProfitSence
Close

Extract the substring between two markers

Last Updated on Wednesday 5th Oct 2022

Python Extract SubString

Regular expressions use the backslash character ('\') to indicate special forms or to allow special characters to be used without invoking their special meaning.A regular expression (or RE) specifies a set of strings that matches it.

  • re — Regular expression operations.
			
					import re
m = re.search('(?<=abc)def', 'abcdef')

print(m.group(0)) # def

			
	

python string extract

			
					import re

text = 'gfgfdAAA1234ZZZuijjk'

try:
    found = re.search('AAA(.+?)ZZZ', text).group(1)
except AttributeError:
    # AAA, ZZZ not found in the original string
    found = '' # apply your error handling

print(found) # 1234

			
	

Python Regex Extract SubString

			
					import re

print(re.findall(r'\d{1,5}','gfgfdAAA1234ZZZuijjk'))

# 1234

			
	

References