HattrickNZ
HattrickNZ

Reputation: 4663

pick out the characters up to a certain character in a string

This is my data

ABCD01MAH_processor_B_stats_qwer_20181105T105946Z.csv ABCD01MAH_processor_B_stats_qwer_20181106T105945Z.csv EFGHIJ01MAH_processor_A_stats_qwer_20181105T105945Z.csv EFGHIJ01MAH_processor_A_stats_qwer_20181106T105945Z.csv

and i want to pick out every thing up to the first underscore

How do I do this?

This is my attempt, using lookahead conditional (?(?=...)yes|no), but it does it up to the last underscore
e.g. (?<name1>\w+(?=_))
https://regex101.com/r/qJ2fL6/1
To get it to pick everything up to the first underscore I have to do the following.
(?<name1>\w+(?=_p)) which works for what I want, in that i get the following:

ABCD01MAH ABCD01MAH EFGHIJ01MAH EFGHIJ01MAH But am I using it right?

How do I get pick up the character, more generally, up to the 1st undrscore? How do I get pick up the character, more generally, up to the 2nd undrscore? How do I get pick up the character, more generally, up to the 3rd undrscore?

example of what I would like:
1st

ABCD01MAH


2nd

ABCD01MAH_processor


3rd

EFGHIJ01MAH_processor_A

Upvotes: 0

Views: 47

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522441

For the first portion we can try using:

^(.*?)(?=_|$).*$

The first capture group would contain the first path. For up to an including the second path, we can use:

^(.*?_.*?)(?=_|$).*$

More generally, for up to and including the Nth term:

^(.*?(?:_.*?){N-1})(?=_|$).*$

So, for 4 terms, N-1 = 3, and we can use:

^(.*?(?:_.*?){3})(?=_|$).*$

Demo

Upvotes: 1

Related Questions