Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

regex - extract word between 2nd underscore and 3rd underscore or space

i want to extract the word after the 2nd & 3rd underscore (_) or the word between the 2nd underscore & space.

my sample data would be:

COL_POS_CJMA_CA_03.09.17
COL_DPU_CJER_CK_03.08.17
COL_POS_CJNE CA_03.09.2017
COL_DPU_CJEK CK_03.08.2017

my results should be:

CJMA
CJER
CJNE
CJEK

I already have this _[^_]*_([^_]*)_ And I could extract the 1st two results. If any one could help me extract the 3rd & 4th results

A regex that says between the 3rd underscore or a space

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

You could go for

^(?:[^_]+_){2}([^_ ]+)

See a demo on regex101.com.


Broken down, this says:
^         # beginning of string
(?:       # non-capturing group
    [^_]+ # not _, at least once
    _     # _
){2}      # repeat the group twice
([^_ ]+)  # capture characters not _ or spaces to group 1

... or, split by _ and analyze the result as an array, ie result[2].


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...