I need a regular expression to convert a string to an anchor tag, which will be used as a hyperlink

Hi I am looking for a regex that will change this:

[check out this URL!](http://www.reallycoolURL.com) 

in it:

 <a href="http://www.reallycoolURL.com">check out this URL</a> 

i.e. the user can enter the URL using my format, and my C # application will convert it to a hyperlink. I want to use the Regex.Replace function inside C #, any help would be appreciated!

+6
c # url regex
source share
4 answers

Use the Regex.Replace method to specify a replacement string that will allow you to format the captured groups. An example is:

 string input = "[check out this URL!](http://www.reallycoolURL.com)"; string pattern = @"\[(?<Text>[^]]+)]\((?<Url>[^)]+)\)"; string replacement = @"<a href=""${Url}"">${Text}</a>"; string result = Regex.Replace(input, pattern, replacement); Console.WriteLine(result); 

Note that I use named capture groups in the template, which allows me to refer to them as ${Name} in the replacement string. You can easily structure a replacement in this format.

Breakdown of the structure:

  • \[(?<Text>[^]]+)] : match the open square bracket and capture everything that is not the closing square bracket into the named group of captured texts. Then match the closing square bracket. Note that the closing square bracket must not be escaped within a character class group. However, it is important to avoid an open square bracket.
  • \((?<Url>[^)]+)\) : the same idea, but with parentheses and capturing to the named Url group.

Named groups help with clarity, and regex patterns can benefit from all the clarity they can get. For completeness, the same approach is used here without using the named groups, in which case they are numbered:

 string input = "[check out this URL!](http://www.reallycoolURL.com)"; string pattern = @"\[([^]]+)]\(([^)]+)\)"; string replacement = @"<a href=""$2"">$1</a>"; string result = Regex.Replace(input, pattern, replacement); Console.WriteLine(result); 

In this case, ([^]]+) is the first group, denoted by $1 in the replacement pattern, and the second group ([^)]+) , referred to by $2 .

+7
source share

Use this regex:

 Regex rx = new Regex(@"\[(?<title>[^]]+)\]\((?<url>[^)]+)\)"); 

And then you can iterate over all matches and get two values:

 foreach(Match match in rx.Matches(yourString)) { string title = match.Groups["title"].Value; string url = match.Groups["url"].Value; string anchorTag = string.Format("<a href=\"{0}\">{1}</a>", url, title); DoSomething(anchorTag); } 
+1
source share

Use this regex:

 ^\[([^\]]+)\]\(([^\)]+)\)$ 

Using this replacement string:

 <href="$2">$1</a> 

Dollar symbols refer to capture groups (these are elements surrounded by open / close brackets) and will retrieve values ​​captured by these groups.

+1
source share

It seems to be something like this help:

 '@((https?://)?([-\w]+\.[-\w\.]+)+\w(:\d+)?(/([-\w/_\.]*(\?\S+)?)?)*)@' 

Found at: http://snipplr.com/view/36992/improvement-of-url-interpretation-with-regex/

-2
source share

All Articles