Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify to only match first occurrence?

Tags:

c#

regex

How can I specify to only match the first occurrence of a regular expression in C# using Regex method?

Here's an example:

string text = @"<link href=""/_layouts/OracleBI/OracleBridge.ashx?RedirectURL=res/sk_oracle10/b_mozilla_4/common.css"" type=""text/css"" rel=""stylesheet""></link></link>"; string pattern = @"(<link).+(link>)"; Regex myRegex = new Regex(pattern, RegexOptions.IgnoreCase);  Match m = myRegex.Match(text);   // m is the first match while (m.Success) {     // Do something with m     Console.Write(m.Value + "\n");     m = m.NextMatch();              // more matches } Console.Read(); 

I would like this to only replace up to the first <\link>. And then also do the same for the rest of these matches.

like image 985
Josh Avatar asked Apr 13 '10 16:04

Josh


People also ask

What is regex Match?

Short for regular expression, a regex is a string of text that lets you create patterns that help match, locate, and manage text. Perl is a great example of a programming language that utilizes regular expressions.

How do I stop regex greedy?

You make it non-greedy by using ". *?" When using the latter construct, the regex engine will, at every step it matches text into the "." attempt to match whatever make come after the ". *?" . This means that if for instance nothing comes after the ".


2 Answers

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.
like image 135
womp Avatar answered Oct 03 '22 06:10

womp


I believe you just need to add a lazy qualifier on the first example. Whenever a wild card is "eating too much", you either need a lazy qualifier on the wild card or, in a more complicated scenario, look ahead. Add a lazy qualifier at the top (.+? in place of .+), and you should be good.

like image 23
Rich Avatar answered Oct 03 '22 08:10

Rich