- docs.python.org –
re
— Regular expression operations
ค้นหาตัวอักษรระหว่าง AAA และ ZZZ
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) # found: 1234
import re text = 'page-0188482009-20211001-0002.json' try: found = re.search('page-\w+-(.+?)-', text).group(1) except AttributeError: found = '' print (found) # found: 20211001
หาชื่อไฟล์ที่อยู่หลัง folder ที่กำหนด
import re text = 'content/ptn_year=2021/ptn_month=10/ptn_day=01/content-20211001.json' try: found = re.search('ptn_day=\w+/(.*)', text).group(1) except AttributeError: found = '' print (found) # found: content-20211001.json