Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It sounds like you're looking for the <code>Replace</code> method with an overload that accepts a MatchEvaluator. The MSDN page for that method can be found <a href="http://msdn.microsoft.com/en-us/library/ms149475.aspx" rel="noreferrer">here</a>.</p> <p>Try this instead:</p> <pre><code>string input = "[img]http://imagesource.com[/img]"; string pattern = @"\[img]([^\]]+)\[\/img]"; string result = Regex.Replace(input, pattern, m =&gt; { var url = m.Groups[1].Value; // do something with url here // return the replace value return @"&lt;img src=""" + url + @""" border=""0"" /&gt;"; }, RegexOptions.IgnoreCase); </code></pre> <p>This uses a multi-statement lambda to simplify working with the group and performing more logic before returning the replacement value. You could, of course, get away with this instead:</p> <pre><code>string result = Regex.Replace(input, pattern, m =&gt; @"&lt;img src=""" + m.Groups[1].Value + @""" border=""0"" /&gt;", RegexOptions.IgnoreCase); </code></pre> <p>In the above case there's no need for the <code>return</code> but it's just returning the original string without additional evaluation. You could stick some ternary operators and add that logic, but it'll look messy. A multi-statement lambda is much cleaner. You may consider breaking it out in its own method, as shown in the aforementioned MSDN link, if it is too large or will be reused in other <code>Regex.Replace</code> efforts.</p> <p>BTW, I also simplified your pattern slightly by removing the escapes for <code>]</code>. Only the opening <code>[</code> needs to be escaped.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload