Posts

Showing posts with the label substring

How to extract a substring from inside a string in Python?

How to extract a substring from inside a string in Python? Let's say I have a string 'gfgfdAAA1234ZZZuijjk' and I want to extract just the '1234' part. 'gfgfdAAA1234ZZZuijjk' '1234' I only know what will be the few characters directly before AAA , and after ZZZ the part I am interested in 1234 . AAA ZZZ 1234 With sed it is possible to do something like this with a string: sed echo "$STRING" | sed -e "s|.*AAA(.*)ZZZ.*|1|" And this will give me 1234 as a result. 1234 How to do the same thing in Python? 12 Answers 12 Using regular expressions - documentation for further reference import re text = 'gfgfdAAA1234ZZZuijjk' m = re.search('AAA(.+?)ZZZ', text) if m: found = m.group(1) # found: 1234 or: import re text = 'gfgfdAAA1234ZZZuijjk' try: found = re.search('AAA(.+?)ZZZ', text).group(1) except Attribute...