如何从 bbcode url 标签中提取 url+参数?
以下代码输出:
http://www.google.com
http://www.google.com&lang
更改代码使其输出的最简单方法是什么:
http://www.google.com
http://www.google.com&lang=en¶m2=this¶m3=that
代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TestRegex9928228
{
class Program
{
static void Main(string[] args)
{
string text1 = "try out [url=http://www.google.com]this site (http://www.google.com)[/url]";
Console.WriteLine(text1.ExtractParameterFromBbcodeUrlElement());
string text2 = "try out [url=http://www.google.com&lang=en¶m1=this¶m2=that]this site (http://www.google.com)[/url]";
Console.WriteLine(text2.ExtractParameterFromBbcodeUrlElement());
Console.ReadLine();
}
}
public static class StringHelpers
{
public static string ExtractParameterFromBbcodeUrlElement(this string line)
{
if (line == null)
return "";
else
{
if (line.Contains("]"))
{
List<string> parts = line.BreakIntoParts(']');
if (parts[0].Contains("="))
{
List<string> sides = parts[0].BreakIntoParts('=');
if (sides.Count > 1)
return sides[1];
else
return "";
}
else
return "";
}
else
return "";
}
}
public static List<string> BreakIntoParts(this string line, char separator)
{
if (String.IsNullOrEmpty(line))
return new List<string>();
else
return line.Split(separator).Select(p => p.Trim()).ToList();
}
}
}
The following code outputs:
http://www.google.com
http://www.google.com&lang
What is the simplest way to change the code so it outputs:
http://www.google.com
http://www.google.com&lang=en¶m2=this¶m3=that
CODE:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TestRegex9928228
{
class Program
{
static void Main(string[] args)
{
string text1 = "try out [url=http://www.google.com]this site (http://www.google.com)[/url]";
Console.WriteLine(text1.ExtractParameterFromBbcodeUrlElement());
string text2 = "try out [url=http://www.google.com&lang=en¶m1=this¶m2=that]this site (http://www.google.com)[/url]";
Console.WriteLine(text2.ExtractParameterFromBbcodeUrlElement());
Console.ReadLine();
}
}
public static class StringHelpers
{
public static string ExtractParameterFromBbcodeUrlElement(this string line)
{
if (line == null)
return "";
else
{
if (line.Contains("]"))
{
List<string> parts = line.BreakIntoParts(']');
if (parts[0].Contains("="))
{
List<string> sides = parts[0].BreakIntoParts('=');
if (sides.Count > 1)
return sides[1];
else
return "";
}
else
return "";
}
else
return "";
}
}
public static List<string> BreakIntoParts(this string line, char separator)
{
if (String.IsNullOrEmpty(line))
return new List<string>();
else
return line.Split(separator).Select(p => p.Trim()).ToList();
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最简单还是最有效?您似乎在问两个不同的问题。最简单的是这样的:
更改:
至:
编辑:看起来您更改了标题以删除“最有效”。这是我看到的最简单的更改(更改的代码行最少)。
Simplest or most efficient? You're asking two different questions it seems. Simplest would be something like this:
Change:
To:
Edit: Looks like you changed title to remove "most efficient". Here's the simplest change (fewest lines of code changed) that I see.