如何取出字符串里面中括号里的内容

hhr1222 2017-05-25 11:03:53
string a="qweqw[as]dasdas[adc]"
结果是:as adc
...全文
5282 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
xuzuning 2017-05-25
  • 打赏
  • 举报
回复
用正则就写作
      string a = "qweqw[as]dasdas[adc]11";
      var r = Regex.Matches(a, @"\[(.+?)\]");
      foreach(Match x in r) Console.WriteLine(x.Groups[1].Value);
xuzuning 2017-05-25
  • 打赏
  • 举报
回复
Split 切割字符串,奇数下标的就是
            string a = "qweqw[as]dasdas[adc]";
            var b = a.Split(new char[] { '[', ']' });
            Console.WriteLine(b[1]); //as
            Console.WriteLine(b[3]); //adc
水哥阿乐 2017-05-25
  • 打赏
  • 举报
回复
呵百花齐放,我也来个VB.net的

Module Module1

    Sub Main()

        Dim Re As System.Text.RegularExpressions.Regex
        Re = New System.Text.RegularExpressions.Regex("(?<=\[)\w+(?=\])")
        Dim Mc As System.Text.RegularExpressions.MatchCollection
        Dim M
        Mc = Re.Matches("qweqw[as]dasdas[adc]")
        For Each M In Mc
            Console.WriteLine(M)
        Next
        Console.ReadKey()
    End Sub

End Module
本人QQ-554433626 2017-05-25
  • 打赏
  • 举报
回复

                string a = "qweqw[as]dasdas[adc]";
                string[] b = a.Split(']');
                StringBuilder sb = new StringBuilder();
                foreach (var c in b)
                {
                    if (c.IndexOf("[") > -1)
                    {
                        sb.Append(c.Substring(c.IndexOf("[") + 1) + " ");
                    }
                }
                Console.WriteLine(sb.ToString());
wanghui0380 2017-05-25
  • 打赏
  • 举报
回复
哦,上面写错了点 改成
 StringCollection resultList = new StringCollection();
            try
            {
                Regex regexObj = new Regex(@"\[(?<result>[^\[\]]+)\]");
                Match matchResult = regexObj.Match(@"qweqw[as]dasdas[adc]");
                while (matchResult.Success)
                {
                    resultList.Add(matchResult.Groups["result"].Value);
                    matchResult = matchResult.NextMatch();
                } 
            }
            catch (ArgumentException ex)
            {
                // Syntax error in the regular expression
            }
wxstar8 2017-05-25
  • 打赏
  • 举报
回复
可以用 IndexOf 获取‘[’的位置,然后从这个位置后面获取字符,遇到']'截至。 string a="qweqw[as]dasdas[adc]" int position = a.IndexOf('[')
wanghui0380 2017-05-25
  • 打赏
  • 举报
回复
正则 表达式: \[(?<result>)[^\[\]]+\] 使用方法
StringCollection resultList = new StringCollection();
try {
	Regex regexObj = new Regex(@"\[(?<result>)[^[\]]+\]");
	Match matchResult = regexObj.Match(subjectString);
	while (matchResult.Success) {
		resultList.Add(matchResult.Groups[1].Value);
		matchResult = matchResult.NextMatch();
	} 
} catch (ArgumentException ex) {
	// Syntax error in the regular expression
}
hhr1222 2017-05-25
  • 打赏
  • 举报
回复
引用 2 楼 DOwnstairs 的回复:
for 根据下标 一个一个的判断。 或者用正则表达式
正则该怎么写
SoulRed 2017-05-25
  • 打赏
  • 举报
回复
for 根据下标 一个一个的判断。 或者用正则表达式
hhr1222 2017-05-25
  • 打赏
  • 举报
回复
1

110,534

社区成员

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

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

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