mono
mono

Reputation: 131

How to find the first x occurrences of a Char in a String using Regex

i'm trying to find out how i can get the first x Matches of a Char in a String. I tried using a Matchcollection but i cant find any escapesequence to stop after the x'd-match.

FYI: I need this for a string with a variable length and a different number of occurences of the searched Char, so just getting all and using only the first x isnt a solution.

Thanks in advance

Edit: I am using steam reader to get information out of a .txt files and write it to a atring, for each file one string. These atrings have very different lengths. In every string are lets say 3 keywords. But sometimes something went wrong and i have only one or two of the keywords. Between the keywords are other fields separated with a ;. So if i use a Matchcollection to get the indexes of the ;'s and one Keyword is missing the Information in the File is shifted. Because of that i need to find the first x occourencces before/after a (existing)keyword.

Upvotes: 0

Views: 541

Answers (4)

Rune FS
Rune FS

Reputation: 21752

The split operation is pretty fast so if the regex is not a requirement this could be used:

public static IEnumerable<int> IndicesOf(this string text, char value, int count)
        {
            var tokens = text.Split(value);
            var sum = tokens[0].Length;
            var currentCount = 0;
            for (int i = 1; i < tokens.Length && 
                            sum < text.Length && 
                            currentCount < count; i++)
            {
                yield return sum;
                sum += 1 + tokens[i].Length;
                currentCount++;
            }
        }

executes in roughly 60% of the time of the regex

Upvotes: 0

V4Vendetta
V4Vendetta

Reputation: 38230

Do you really want to use Regex, something like this won't do ?

string simpletext = "Hello World";
int firstoccur = simpletext.IndexOfAny(new char[]{'o'});

Since you want all the indexes for that character you can try in this fashion

string simpletext = "Hello World";
int[] occurences = Enumerable.Range(0, simpletext.Length).Where(x => simpletext[x] == 'o').ToArray();

Upvotes: 1

Russ Clarke
Russ Clarke

Reputation: 17909

If you're determined to use Regex then I'd do this with Matches as opposed to Match actually; largely because you get the count up front.

string pattern = "a";
string source = "this is a test of a regex match";
int maxMatches = 2;

MatchCollection mc = Regex.Matches(source, pattern);

if (mc.Count() > 0)
{
  for (int i = 0; i < maxMatches; i++) 
  {
    //do something with mc[i].Index, mc[i].Length
  }
}

Upvotes: 0

stema
stema

Reputation: 93086

You can use the class Match. this class returns only one result, but you can iterate over the string till it found the last one.

Something like this:

Match match = Regex.Match(input, pattern);
int count = 0;

while (match.Success)
{
    count++;

    // do something with match

    match = match.NextMatch();

    // Exit the loop when your match number is reached
}

Upvotes: 0

Related Questions