有个数据采集后得到的dat文件,用txt打开是如下== Data Management Software CL-S1w DATA FILE <1.31> ==
[DATE]
11/29/2006 20:10:05
[LAYOUT FILE]
C:\Documents and Settings\2410\Desktop\Try_it2.cll
[DATA R1-8]
R1,
R2,
R3,
R4,
R5,
R6,
R7,
R8,[White]
No, X, Y, Z, Ev, x, y, Ev, u', v', Ev, Tcp,delta uv
00,+00025.8,+00028.2,+00023.8,+00028.2,U0.3314,U0.3623,+00028.2,U0.1983,U0.4877,+00028.2,+05557,+0.0110
01,------.-,------.-,------.-,------.-,--.----,--.----,------.-,--.----,--.----,------.-,------,--.----
02,------.-,------.-,------.-,------.-,--.----,--.----,------.-,--.----,--.----,------.-,------,--.----
03,------.-,------.-,------.-,
后头还有很多无用的数据请大家看字符串第19行有2个"U"开头的数据,U0.3314,U0.3623 这就是我要提出来的x和y值,这个按说用正则表达式应该比较方便,只是不熟悉正则写法,请给与帮助。
[DATE]
11/29/2006 20:10:05
[LAYOUT FILE]
C:\Documents and Settings\2410\Desktop\Try_it2.cll
[DATA R1-8]
R1,
R2,
R3,
R4,
R5,
R6,
R7,
R8,[White]
No, X, Y, Z, Ev, x, y, Ev, u', v', Ev, Tcp,delta uv
00,+00025.8,+00028.2,+00023.8,+00028.2,U0.3314,U0.3623,+00028.2,U0.1983,U0.4877,+00028.2,+05557,+0.0110
01,------.-,------.-,------.-,------.-,--.----,--.----,------.-,--.----,--.----,------.-,------,--.----
02,------.-,------.-,------.-,------.-,--.----,--.----,------.-,--.----,--.----,------.-,------,--.----
03,------.-,------.-,------.-,
后头还有很多无用的数据请大家看字符串第19行有2个"U"开头的数据,U0.3314,U0.3623 这就是我要提出来的x和y值,这个按说用正则表达式应该比较方便,只是不熟悉正则写法,请给与帮助。
MatchCollection mc = Regex.Matches(yourStr, "(.+?,){5}(?<x>u.+?),(?<y>u.+?),(.+?,){5}.+\\r\\n", RegexOptions.IgnoreCase);
foreach(Match m in mc)
{
m.Groups["x"].Value;
m.Groups["y"].Value;
}
{
// Create an instance of StreamReader to read from a file.
// The using statement also closes the StreamReader.
using (StreamReader sr = new StreamReader(datPath.Text))
{
datFileString = sr.ReadToEnd();
MatchCollection mc = Regex.Matches(datFileString, "(.+?,){5}(?<x>u.+?),(?<y>u.+?),(.+?,){5}.+\\r\\n", RegexOptions.IgnoreCase);
foreach(Match m in mc)
{
xVal.Text = m.Groups["x"].Value;
yVal.Text = m.Groups["y"].Value;
} }
}我这样写进去了,匹配的那步时间好长,最后也没有结果。
头一次用这个,可能会犯低级错误,请大侠们指教。
StreamReader sr = new StreamReader(path);
//字符串过长也是造成匹配时间过长的原因,这里你可以看一下这个string有多长
datFileString = sr.ReadToEnd();
//int length = datFileString.Length;
sr.Close();//用过了就关掉,等待GC自动回收
MatchCollection mc = Regex.Matches(datFileString, "(.+?,){5}(?<x>u.+?),(?<y>u.+?),(.+?,){5}.+\\r\\n", RegexOptions.IgnoreCase);
//我这里匹配的可是这个字符串里面所有匹配项,也就是可能不止一个,如果只有一个,就可以不用MatchCollection
//直接Match m = Regex.Match(...)
//m.Groups["x"].Value......
//就行了。
foreach(Match m in mc)
{
xVal.Text = m.Groups["x"].Value;
yVal.Text = m.Groups["y"].Value;
}
但是用你的串就没有结果。我这两个数据是一对x,y,也就是只会出现两次。当让我想你的方法是为了使代码更健壮。因为是使用不久,只是在某个文章里头看到using以后,就会自动处理的,所以没有close,其中的原理我不懂。