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
445 views
in Technique[技术] by (71.8m points)

Regex Split String on Delimiter

My data is underscore-delimited like this for example:

A_B_C_D_E

Regex 1 I need to match: A__C_D_E

Regex 2 I need to match: A_B__D_E

Regex 3 I need to match: A_B_C__E

A little background on this. I'll be replacing the regex matches with empty.string thus leaving me with the value of each column. Example, regex 1 would leave me with the value B when the match is replaced by empty.string. IMPORTANT: ABCDE are all dyanmic data...they are not literally ABCDE. The underscore delimiter is literal. The number of "columns" delimited by this underscore is constant.

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

For case 1:

Search using:

^([^_]*_)[^_]*

And replace by:

$1

RegEx Demo 1


For case 2:

Search using:

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

And replace by:

$1

RegEx Demo 2


For case 3:

Search using:

^((?:[^_]*_){3})[^_]*

And replace by:

$1

RegEx Demo 3


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

...