简体   繁体   中英

get all the string before first numeric character python regex

GC_DATA_TEXT_20200304.csv

get only

GC_DATA_TEXT

case insensitive

You could use re.sub here:

inp = "GC_DATA_TEXT_20200304.csv"
out = re.sub(r'_\d{8}.*$', '', inp)
print(out)

Another possibility would be to split the string on the third underscore, and then join together again:

out = "_".join(inp.split("_", 3)[:3])
print(out)

Both print:

GC_DATA_TEXT
import re
text='GC_DATA_TEXT_20200304.csv'
pattern = r'(\D+)_'
matched_ans=re.findall(pattern,text)[0]
'GC_DATA_TEXT'

/D+ should match all non numeric characters

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM