简体   繁体   English

我需要一个正则表达式将字符串转换为锚标记以用作超链接

[英]I need a regular expression to convert a string into an anchor tag to be used as a hyperlink

Hi I'm looking a regular expression that will change this: 嗨,我正在寻找一个会改变这种情况的正则表达式:

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

into this: 进入这个:

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

ie a user can input a URL using my format and my C# application will convert this into a hyperlink. 即用户可以使用我的格式输入URL,我的C#应用​​程序会将其转换为超链接。 I'm looking to make use of the Regex.Replace function within C#, any help would be appreciated! 我想在C#中使用Regex.Replace函数,任何帮助都将不胜感激!

Use the Regex.Replace method to specify a replacement string that will allow you to format the captured groups. 使用Regex.Replace方法指定替换字符串,以允许您格式化捕获的组。 An example would be: 一个例子是:

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);

Notice that I am using named capture groups in the pattern, which allows me to refer to them as ${Name} in the replacement string. 请注意,我在模式中使用了命名捕获组,这允许我在替换字符串中将它们称为${Name} You can structure the replacement easily with this format. 您可以使用此格式轻松构建替换。

The pattern breakdown is: 模式细分是:

  • \\[(?<Text>[^]]+)] : match an opening square bracket, and capture everything that is not a closing square bracket into the named captured group Text . \\[(?<Text>[^]]+)] :匹配一个左方括号,并将不是结束方括号的所有内容捕获到指定的捕获组Text中 Then match the closing square bracket. 然后匹配关闭的方括号。 Notice that the closing square bracket need not be escaped within the character class group. 请注意,关闭方括号不需要在字符类组中进行转义。 It is important to escape the opening square bracket though. 尽管逃离开口方括号是很重要的。
  • \\((?<Url>[^)]+)\\) : same idea but with parentheses and capturing into the named Url group. \\((?<Url>[^)]+)\\) :同样的想法,但带括号并捕获到命名的Url组。

Named groups help with clarity, and regex patterns can benefit from all the clarity they can get. 命名组有助于清晰,正则表达式可以从他们可以获得的所有清晰度中受益。 For the sake of completeness here is the same approach without using 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, referred to via $1 in the replacement pattern, and the second group is ([^)]+) , referred to by $2 . 在这种情况下([^]]+)是第一个组,在替换模式中通过$1引用,第二个组是([^)]+) ,由$2引用。

Use this regex: 使用这个正则表达式:

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

And then you can iterate through all matches and get the 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);
}

Use this regex: 使用这个正则表达式:

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

With this replacement string: 使用此替换字符串:

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

The dollar symbols refer to the capturing groups (these are the items surrounded by the open/close parentheses) and will extract the values captured by these groups. 美元符号表示捕获组(这些是由打开/关闭括号括起来的项目),并将提取这些组捕获的值。

It looks like something like this help: 它看起来像这样的帮助:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM