Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.9k views
in Technique[技术] by (71.8m points)

regex - python regular expression 6th underscore

x = '1P2VI_R072_PH_INSPECT_VIS_{}_**P018**_DX16DY12SDX8SDY1_2020-07-26_14-53-24.JPG'

I am looking to extract 1P2VI and P018 1st 5 letters and P016 (after 6th underscore) I got it by a,b = x[:5] , x[29:33] but there are data like

 'V92EM_R071_PH_INSPECT_VIS_2_**10**_210105_133452.JPG'

So how do I extract character that starts after 6th underscore and ends after 6th underscore

need P018 and 10 for above data using python


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If the first five characters are also followed by an underscore, you should just use str.split:

w = x.split('_')
print([w[0],w[6]])

Output (for your sample data):

['1P2VI', 'P018']
['V92EM', '10']

If not, you can use this regex, which captures the first 5 characters, then skips the next 6 groups of characters ending in an underscore, and then captures the characters up to the next underscore:

^(.{5})(?:[^_]*_){6}([^_]*)

In python:

w = re.search(r'^(.{5})(?:[^_]*_){6}([^_]*)', x)
print([w[1],w[2]])

Output:

['1P2VI', 'P018']
['V92EM', '10']

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...