Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
442 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 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...