Anu Viswan
Anu Viswan

Reputation: 18155

RegEx string between N and (N+1)th Occurance

I am attempting to find nth occurrence of sub string between two special characters. For example. one|two|three|four|five

Say, I am looking to find string between (n and n+1 th) 2nd and 3rd Occurrence of '|' character, which turns out to be 'three'.I want to do it using RegEx. Could someone guide me ?

My Current Attempt is as follows.

        string subtext = "zero|one|two|three|four";
        Regex r = new Regex(@"(?:([^|]*)|){3}");
        var m = r.Match(subtext).Value;

Upvotes: 0

Views: 1695

Answers (3)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626845

If you have full access to C# code, you should consider a mere splitting approach:

var idx = 2; // Might be user-defined
var subtext = "zero|one|two|three|four";
var result = subtext.Split('|').ElementAtOrDefault(idx);
Console.WriteLine(result);
// => two

A regex can be used if you have no access to code (if you use some tool that is powered with .NET regex):

^(?:[^|]*\|){2}([^|]*)

See the regex demo. It matches

  • ^ - start of string
  • (?:[^|]*\|){2} - 2 (or adjust it as you need) or more sequences of:
    • [^|]* - zero or more chars other than |
    • \| - a | symbol
  • ([^|]*) - Group 1 (access via .Groups[1]): zero or more chars other than |

C# code to test:

var pat = $@"^(?:[^|]*\|){{{idx}}}([^|]*)";
var m = Regex.Match(subtext, pat);
if (m.Success) {
    Console.WriteLine(m.Groups[1].Value);
}
// => two

See the C# demo

If a tool does not let you access captured groups, turn the initial part into a non-consuming lookbehind pattern:

(?<=^(?:[^|]*\|){2})[^|]*
^^^^^^^^^^^^^^^^^^^^

See this regex demo. The (?<=...) positive lookbehind only checks for a pattern presence immediately to the left of the current location, and if the pattern is not matched, the match will fail.

Upvotes: 4

CinCout
CinCout

Reputation: 9619

Use this: (?:.*?\|){n}(.[^|]*)

where n is the number of times you need to skip your special character. The first capturing group will contain the result.

Demo for n = 2

Upvotes: 1

adjan
adjan

Reputation: 13652

Use this regex and then select the n-th match (in this case 2) from the Matches collection:

string subtext = "zero|one|two|three|four";
Regex r = new Regex("(?<=\|)[^\|]*");
var m = r.Matches(subtext)[2];

Upvotes: 1

Related Questions