求写一个正则表达式,用来分割字符串

宇峰科技 2013-01-05 02:17:22
["放上110kV母差保护110kV母分开关大电流试验端子流变"侧短"接螺丝,取下连接螺丝"][2][21000000010014][6,5]

将上面这句话分割成如下

放上110kV母差保护110kV母分开关大电流试验端子流变"侧短"接螺丝,取下连接螺丝
2
21000000010014
6,5

就是将中括号内的提出来,谢谢。
在线等待.
...全文
184 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
linuxyf 2013-01-05
  • 打赏
  • 举报
回复
String subjectString = "[\"放上110kV母差保护110kV母分开关大电流试验端子流变\"侧短\"接螺丝,取下连接螺丝\"][2][21000000010014][6,5]"; StringCollection resultList = new StringCollection(); try { Regex regexObj = new Regex(@"\[""?(.*?)""?\]"); 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 } }
宇峰科技 2013-01-05
  • 打赏
  • 举报
回复
引用 7 楼 lgyg2002 的回复:
C# code?12string text="[\"放上110kV母差保护110kV母分开关大电流试验端子流变\"侧短\"接螺丝,取下连接螺丝\"][2][21000000010014][6,5]";string[] sary = text.Split(new string[] { "[", "]" }, StringSplitOptions.RemoveEmptyEnt……
很好,很强大
Assassin_ 2013-01-05
  • 打赏
  • 举报
回复
正则不会.split的

        public void test()
        {
            string str = "[\"放上110kV母差保护110kV母分开关大电流试验端子流变\"侧短\"接螺丝,取下连接螺丝\"][2][21000000010014][6,5]";
           // string RepStr = str.Replace("][", "|");
            string[] WriteStr = str.Split(new string[] { "]","[" }, StringSplitOptions.RemoveEmptyEntries);

            foreach (var arrString in WriteStr)
            {
                Response.Write(arrString + "<br />");
            }
        }
宇峰科技 2013-01-05
  • 打赏
  • 举报
回复
引用 2 楼 bdmh 的回复:
本帖最后由 bdmh 于 2013-01-05 14:22:26 编辑 C# code?12345678 StreamReader reader = new StreamReader("c:\\temp\\1.txt",Encoding.Default); string source = read……
这个可以,不过最外层的引号没有去掉
lgyg2002 2013-01-05
  • 打赏
  • 举报
回复

string text="[\"放上110kV母差保护110kV母分开关大电流试验端子流变\"侧短\"接螺丝,取下连接螺丝\"][2][21000000010014][6,5]";
string[] sary = text.Split(new string[] { "[", "]" }, StringSplitOptions.RemoveEmptyEntries);

宇峰科技 2013-01-05
  • 打赏
  • 举报
回复
我试试
yixu616 2013-01-05
  • 打赏
  • 举报
回复
string sTmp = "[\"放上110kV母差保护110kV母分开关大电流试验端子流变\"侧短\"接螺丝,取下连接螺丝\"][2][21000000010014][6,5]"; string sReg = "\\[.*?\\]"; MatchCollection mactTag = Regex.Matches(sTmp, sReg);
  • 打赏
  • 举报
回复
string str = @"[""放上110kV母差保护110kV母分开关大电流试验端子流变""侧短""接螺丝,取下连接螺丝""][2][21000000010014][6,5]"; var ary = Regex.Matches(str, @"(?<=\[(""?))[^\]]+(?=\1\])").OfType<Match>().Select(t => t.Value).ToArray();
宇峰科技 2013-01-05
  • 打赏
  • 举报
回复
怎么没人回啊。。。。
bdmh 2013-01-05
  • 打赏
  • 举报
回复

StreamReader reader = new StreamReader("c:\\temp\\1.txt",Encoding.Default);
string source = reader.ReadToEnd();
Regex reg = new Regex(@"(?is)(?<=\[)[^]]+(?=\])");
MatchCollection mc = reg.Matches(source);
foreach (Match m in mc)
{
MessageBox.Show(m.Value);
}
宇峰科技 2013-01-05
  • 打赏
  • 举报
回复
中括号内的内容是不确定的,都是任意字符串

110,538

社区成员

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

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

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