本文介绍了如何使用C#在两个字符之间提取字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我自动生成的HTML源文本文件如下所示: <! - - // - >< / SCRIPT> < script language = JavaScript> var staMgrParaInf = new 数组( 1 , 0 , 0 , 0 , 10 , 48 , 2 , 1 , 0 , 0 , 0 ); < / script>< script language = JavaScript> var staEntryInf = new 数组( 192.168.1.56, 0 , 87357 , 111282 , 10432836 , 103871290 , 1 , 1 , 41 , 52 , 192.168.1.21, 0 , 394 , 42991358 , 55037052 , 5 , 7 , 260 , 10360 );< / script> < / HEAD > < SCRIPT language = javascript> var pageRefreshTime = 10 ; 我想只提取这个文字 192.168.1.56, 0 , 87357 , 111282 , 10432836 , 103871290 , 1 , 1 , 41 , 52 , 192.168.1.21, 0 , 39439 , 42063 , 2991358 , 55037052 , 5 , 7 , 260 , 10360 i试试这个代码 private void button1_Click( object sender, EventArgs e) { String 行; 字符串 filetext = null ; int count = 0 ; 使用(System.IO.StreamReader reader = new System.IO.StreamReader( C:\\viewsource.txt)) { while ((line = reader.ReadLine())!= null ) { if (count == 0 ) { if (line.StartsWith( < )) { } else { filetext = filetext + line; } } else { if (line.StartsWith( <)) { } else { filetext = filetext + \ n + line; } } count ++; } Trace.WriteLine(filetext); int start = filetext.IndexOf( ()+ 1 ; int end = filetext .IndexOf( ),start); string result = filetext.Substring(start,end - start); textBox1.Text = result; reader.Close(); } } 我的结果只有这个 1 , 0 , 0 , 0 , 10 , 48 , 2 , 1 , 0 , 0 , 0 i只想提取这个文本 192.168.1.56, 0 , 87357 , 111282 , 10432836 , 103871290 , 1 , 1 , 41 , 52 , 192.168.1.21, 0 , 39439 , 42063 , 2991358 , 55037052 , 5 , 7 , 260 , 10360 解决方案 尝试: (?< = Array\()。+(?= \)) 试试这个正则表达式模式: \ W((?:\\\ {1,3} \。){3} \d {1,3}。 。(?: \d {1,9},){8} \d {1,9})\W i have auto generated HTML source text file look like this:<!--//--></SCRIPT><script language=JavaScript>var staMgrParaInf = new Array(1,0,0,0,10,48,2,1,0,0,0 );</script><script language=JavaScript>var staEntryInf = new Array("192.168.1.56",0,87357,111282,10432836,103871290,1,1,41,52,"192.168.1.21",0,394,42991358,55037052,5,7,260,10360);</script></HEAD><SCRIPT language="javascript"> var pageRefreshTime = 10;i want to extract only this text"192.168.1.56",0,87357,111282,10432836,103871290,1,1,41,52,"192.168.1.21",0,39439,42063,2991358,55037052,5,7,260,10360i try this codeprivate void button1_Click(object sender, EventArgs e){ String line; String filetext = null; int count = 0; using (System.IO.StreamReader reader = new System.IO.StreamReader("C:\\viewsource.txt")) { while ((line = reader.ReadLine()) != null) { if (count == 0) { if (line.StartsWith("<")) { } else { filetext = filetext + line; } } else { if (line.StartsWith("<")) { } else { filetext = filetext + "\n" + line; } } count++; } Trace.WriteLine(filetext); int start = filetext.IndexOf("(") + 1; int end = filetext.IndexOf(")", start); string result = filetext.Substring(start, end - start); textBox1.Text = result; reader.Close(); }}My result only this1,0,0,0,10,48,2,1,0,0,0i want to extract only this text"192.168.1.56",0,87357,111282,10432836,103871290,1,1,41,52,"192.168.1.21",0,39439,42063,2991358,55037052,5,7,260,10360 解决方案 Try:(?<=Array\()".+(?=\))Try this regex pattern: \W((?:\d{1,3}\.){3}\d{1,3}..(?:\d{1,9},){8}\d{1,9})\W 这篇关于如何使用C#在两个字符之间提取字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-12 17:15