正则表达式 提取字符串问题?

hoho29 2011-03-18 05:48:45
aaa[bbb|http://www.csdn.com]
这样一段字符串,怎么通过正则表达式把bbb和http://www.csdn.com给提取出来?
...全文
142 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
huangwenquan123 2011-03-19
  • 打赏
  • 举报
回复
            string str = "aaa[bbb|http://www.csdn.com]aaa[hhhh[bbb|http://www.csdn.com]";
Regex reg = new Regex(@"(((?<Open>\[)([^\[\]]*))*((?<-Open>\])[^\[\]]*)*)*(?(Open)(?!))");
foreach (Match m in reg.Matches(str))
{
string[] ss = m.Groups[3].Value.Split('|');
foreach (string s in ss)
{
Response.Write(s);
Response.Write("<br/>");
}
}
/*
bbb
http://www.csdn.com

bbb
http://www.csdn.com
*/
h2ero 2011-03-19
  • 打赏
  • 举报
回复
楼主看看这个吧


string s = "aaa[bbb|http://www.csdn.com]aaa[hhhh[bbbc|http://www.csdn.com]";
Regex re = new Regex(@".*?(\[|\[(.*?))(?<result>\w+?)\|(?<result2>.*?)\].*?");
MatchCollection result = re.Matches(s);
foreach (Match item in result)
{
Console.WriteLine(item.Groups["result"] + "\n" + item.Groups["result2"]);
}
Console.ReadLine();

hoho29 2011-03-18
  • 打赏
  • 举报
回复
有没有最佳答案呀?
hoho29 2011-03-18
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 dongxinxi 的回复:]
C# code

string str = @"aaa[bbb|http://www.csdn.com]";
string[] result = new Regex(@"(?is)(?<=\[)\S+(?=\])").Match(str).Value.Split('|');
result[0] //bbb
result[1]; //http...
[/Quote]




aaa[bbb|http://www.csdn.com]aaa[hhhh[bbb|http://www.csdn.com]
这样的字符串就取不到
xiangyun_1224 2011-03-18
  • 打赏
  • 举报
回复
string str = @"aaa[bbb|http://www.csdn.com]";
result = new Regex(@"(?is)(?<=\[)\S+(?=\])").Match(str).Value.Split('|');
alert(result);
xiangyun_1224 2011-03-18
  • 打赏
  • 举报
回复
有一条正则表达式(找出所有链接地址): 1.<a[^<>]href=[\"|'|]?[^\"'<>]+[\"|']?>

你要在Groups通过标记来取, 你得先在表达式里标明标记(?<标记>(内容))

变为

2.<a[^<>]href=[\"|'|]?(?<HREF>[^\"'<>]+)[\"|']?>

对比一下1和2有什么不同.

foreach(Match match in matches)
{
s += match.Groups["HREF"].Value;
}
  • 打赏
  • 举报
回复

string str = @"aaa[bbb|http://www.csdn.com]";
string[] result = new Regex(@"(?is)(?<=\[)\S+(?=\])").Match(str).Value.Split('|');
result[0] //bbb
result[1]; //http...
兔子-顾问 2011-03-18
  • 打赏
  • 举报
回复
Match m = Regex.Match(yourStr,@"\[([^|]+)\|([^\]]+)\]");
m.Groups[1].Value;//就是你要的第一部分
m.Groups[2].Value;//就是你要的第二部分
hoho29 2011-03-18
  • 打赏
  • 举报
回复
aaa[bbb|http://www.csdn.com]aaa[hhhh[bbb|http://www.csdn.com]
又怎么取了?
zzmsyt 2011-03-18
  • 打赏
  • 举报
回复
慢慢分割
hoho29 2011-03-18
  • 打赏
  • 举报
回复
aaa[bbb|http://www.csdn.com]aaa[bbb|http://www.csdn.com]
怎么取?
hoho29 2011-03-18
  • 打赏
  • 举报
回复
[]里的内容怎么取了?
旅行者I号 2011-03-18
  • 打赏
  • 举报
回复
你是想取[]里面的内容,再按|分割吧

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧