3

I'm parsing a BB code image tag:

[img]http://imagesource.com[/img]

I'm using the following Replace() function:

Regex.Replace(msg, @"\[img\]([^\]]+)\[\/img\]", @"<img src=""$1"" border=""0"" />", RegexOptions.IgnoreCase);

And I need to get the URL while parsing. I need to know the value of "$1". Is it possible? The Regex class somehow replaces the "$1" string with the value that I need, so there must a way to get it.

1

3 Answers 3

7

It sounds like you're looking for the Replace method with an overload that accepts a MatchEvaluator. The MSDN page for that method can be found here.

Try this instead:

string input = "[img]http://imagesource.com[/img]";
string pattern = @"\[img]([^\]]+)\[\/img]";
string result = Regex.Replace(input, pattern, m =>
    {
        var url = m.Groups[1].Value;
        // do something with url here
        // return the replace value
        return @"<img src=""" + url + @""" border=""0"" />";
     },
    RegexOptions.IgnoreCase);

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:

string result = Regex.Replace(input, pattern,
    m => @"<img src=""" + m.Groups[1].Value + @""" border=""0"" />",
    RegexOptions.IgnoreCase);

In the above case there's no need for the return 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 Regex.Replace efforts.

BTW, I also simplified your pattern slightly by removing the escapes for ]. Only the opening [ needs to be escaped.

Sign up to request clarification or add additional context in comments.

2 Comments

It looks like the first group contains the whole string, that's why he used m.Groups[1]
@reggaeguitar that's correct. The group at index 0 contains the entire match.
0

To keep the captured string, just "capture" the return value.

 string s = Regex.Replace(msg, @"\[img\]([^\]]+)\[\/img\]", @"<img src=""$1"" border=""0"" />", RegexOptions.IgnoreCase); 

Comments

0

Capture groups are available in the Captures property for the Regex match, if you do a match instead of a replace, then you'll have access to the group.

1 Comment

Sorry, I listed the property. The Captures property has all capture groups, Groups are the named Capture groups. If there aren't any named ones, then Groups[0] is the last matched capture.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.