0

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

HattrickNZ
  • 4,373
  • 15
  • 54
  • 98
  • What programming language/regex tool are you using? The exact answer you need really depends on this. – Tim Biegeleisen Nov 08 '18 at 01:14
  • Have you tried `^(((\w+?)_\w+?)_\w+?)_`? Three Capturing groups will pull up to 1st, 2nd, and 3rd. [See it here](https://regex101.com/r/qJ2fL6/4) – K.Dᴀᴠɪs Nov 08 '18 at 01:25

1 Answers1

1

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

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • Doesn't matter much, when possible, but negative character sets are [more efficient](https://stackoverflow.com/questions/22444/my-regex-is-matching-too-much-how-do-i-make-it-stop#comment93020571_22457) than lazy repitition, see https://regex101.com/r/KYn8Sq/2 – CertainPerformance Nov 08 '18 at 01:33