Home > OS >  Regex to match words between underscores after second occurence of underscore
Regex to match words between underscores after second occurence of underscore

Time:02-21

so i would like to get words between underscores after second occurence of underscore

this is my string

ABC_BC_BE08_C1000004_0124

I've assembled this expresion

(?<=_)[^_]

well it matches what i need but only skips the first word since there is no underscore before it. I would like it to skip ABC and BC and just get the last three strings, i've tried messing around but i am stuck and cant make it work. Thanks!

CodePudding user response:

You can use a non-regex approach here with Split and Skip:

var text = "ABC_BC_BE08_C1000004_0124";
var result = text.Split('_').Skip(2);
foreach (var s in result)
    Console.WriteLine(s);

Output:

BE08
C1000004
0124

See the C# demo.

With regex, you can use

var result = Regex.Matches(text, @"(?<=^(?:[^_]*_){2,})[^_] ").Cast<Match>().Select(x => x.Value);

See the regex demo and the C# demo. The regex matches

  • (?<=^(?:[^_]*_){2,}) - a positive lookbehind that matches a location that matches the following patterns immediately to the left of the current location:
    • ^ - start of string
    • (?:[^_]*_){2,} - two or more ({2,}) sequences of any zero or more chars other than _ ([^_]*) and then a _ char
  • [^_] - one or more chars other than _
  • Related