How to specify to only match first occurrence?

Regex.Match(myString) returns the first match it finds.

Subsequent calls to NextMatch() on the resultant object from Match() will continue to match the next occurrences, if any.

For example:

  string text = "my string to match";
  string pattern = @"(\w+)\s+";
  Regex myRegex = new Regex(pattern, RegexOptions.IgnoreCase);

  Match m = myRegex.Match(text);   // m is the first match
  while (m.Success)
  {
       // Do something with m

       m = m.NextMatch();              // more matches
  }


EDIT: If you’re parsing HTML, I would seriously consider using the HTML Agility Pack. You will save yourself many, many headaches.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)