Max
Max

Reputation: 1309

C# need regular expression to capture the second occurence after a underscore

I am using the regular expression below to capture all numbers/letters after a underscore but I need to capture only the second occurence i.e "00500" as you see below:

regular expresion: (?<=_)[a-zA-Z0-9]+

string:
"-rw-rw-rw- 1 rats rats 31K Sep 17 13:33 /opt/data/automation_sent/20180918/labc/0/20180918_00500.itx"

I am doing in C# and I thought the value would be in the second group[1] but it is not; it only captures the string "_sent":

        string temp2 = "";
        Regex getValueAfterUnderscore = new Regex(@"(?<=_)[a-zA-Z0-9]+");
        Match match2 = getValueAfterUnderscore.Match(line);
        if (match2.Success)
        {
            temp2 = match2.Groups[1].Value;
            Console.WriteLine(temp2); 
        }

Any ideas? Thank you!

Upvotes: 1

Views: 284

Answers (4)

Ahmed Yousif
Ahmed Yousif

Reputation: 2348

you can use the following code that capture text after second underscore

 var line = "-rw-rw-rw- 1 rats rats 31K Sep 17 13:33 /opt/data/automation_sent/20180918/labc/0/20180918_00500.itx";
            string temp2 = "";
            Regex getValueAfterUnderscore = new Regex(@"_.+_([a-zA-Z0-9]+)");
            Match match2 = getValueAfterUnderscore.Match(line);
            if (match2.Success)
            {
                temp2 = match2.Groups[1].Value;
                Console.WriteLine(temp2); 
            }

output:

00500

Upvotes: 2

toumir
toumir

Reputation: 641

var input = "-rw-rw-rw- 1 rats rats 31K Sep 17 13:33 /opt/data/automation_sent/20180918/labc/0/20180918_00500.itx";
    Regex regex = new Regex(@"(?<Identifier1>\d+)_(?<Identifier2>\d+)");
    var results = regex.Matches(input);
    foreach (Match match in results)
    {
        Console.WriteLine(match.Groups["Identifier1"].Value);
        Console.WriteLine(match.Groups["Identifier2"].Value);//second occurence
    }

tested here : http://rextester.com/SIMXNS63534

Upvotes: 1

vinicius.ras
vinicius.ras

Reputation: 1626

Maybe you are confusing "groups" with "matches". You should search for matches of your regular expression. Here's how to list all matches of your regex in a given string:

string str = "-rw-rw-rw- 1 rats rats 31K Sep 17 13:33 /opt/data/automation_sent/20180918/labc/0/20180918_00500.itx";

MatchCollection matches = Regex.Matches(str, @"(?<=_)[a-zA-Z0-9]+");
foreach (Match curMatch in matches)
    Console.WriteLine(curMatch.Value);

For your specific case, verify if there are at least 2 matches and retrieve the value of matches[1] (which is the second match).

if (matches.Count >= 2)
    Console.WriteLine($"Your result: {matches[1].Value}");

Upvotes: 1

Jack Sparrow
Jack Sparrow

Reputation: 607

If all your strings look like this pattern {SOME_STRING}_{YOUR_NUMBER}.itx, then you can use this solution (without regex)

var arr = str.Split(new[] {"_", ".itx"}, StringSplitOptions.RemoveEmptyEntries);
var result = arr[arr.Length - 1];

Upvotes: 0

Related Questions