本人想匹配 %R1P,0,0:[整数],[任意实数],[任意实数],[任意实数],[任意实数]
输入 %R1P,0,0:0,0,0,0,0
//代码如下
string str = @”%R1P,0,0:(\d+),(\-?\d+(\.\d+)?),(\-?\d+(\.\d+)?),(\-?\d+(\.\d+)?),(\-?\d+(\.\d+)?)”;
if (Regex.IsMatch(Receive, str))
{
Match match = Regex.Match(Receive, str);//能匹配成功
SR = (ExecuteResult)Convert.ToInt32(match.Groups[1].Value);// SR =0
stationE = Convert.ToDouble(match.Groups[2].Value);// stationE = 0
stationN = Convert.ToDouble(match.Groups[3].Value);// stationN =””; 不是本人想要的
stationZ = Convert.ToDouble(match.Groups[4].Value);// stationZ =0;
stationHight = Convert.ToDouble(match.Groups[5].Value);// stationHight=””;不是本人想要的
return true;
}
//问一下这个代码应该怎么改?
输入 %R1P,0,0:0,0,0,0,0
//代码如下
string str = @”%R1P,0,0:(\d+),(\-?\d+(\.\d+)?),(\-?\d+(\.\d+)?),(\-?\d+(\.\d+)?),(\-?\d+(\.\d+)?)”;
if (Regex.IsMatch(Receive, str))
{
Match match = Regex.Match(Receive, str);//能匹配成功
SR = (ExecuteResult)Convert.ToInt32(match.Groups[1].Value);// SR =0
stationE = Convert.ToDouble(match.Groups[2].Value);// stationE = 0
stationN = Convert.ToDouble(match.Groups[3].Value);// stationN =””; 不是本人想要的
stationZ = Convert.ToDouble(match.Groups[4].Value);// stationZ =0;
stationHight = Convert.ToDouble(match.Groups[5].Value);// stationHight=””;不是本人想要的
return true;
}
//问一下这个代码应该怎么改?
解决方案:15分
楼上正解,正则分组序号由左括号的序号确定:
0:%R1P,0,0:(\d+),(\-?\d+(\.\d+)?),(\-?\d+(\.\d+)?),(\-?\d+(\.\d+)?),(\-?\d+(\.\d+)?)
1:%R1P,0,0:(\d+),(\-?\d+(\.\d+)?),(\-?\d+(\.\d+)?),(\-?\d+(\.\d+)?),(\-?\d+(\.\d+)?)
2:%R1P,0,0:(\d+),(\-?\d+(\.\d+)?),(\-?\d+(\.\d+)?),(\-?\d+(\.\d+)?),(\-?\d+(\.\d+)?)
3:%R1P,0,0:(\d+),(\-?\d+(\.\d+)?),(\-?\d+(\.\d+)?),(\-?\d+(\.\d+)?),(\-?\d+(\.\d+)?)
4:%R1P,0,0:(\d+),(\-?\d+(\.\d+)?),(\-?\d+(\.\d+)?),(\-?\d+(\.\d+)?),(\-?\d+(\.\d+)?)
5:%R1P,0,0:(\d+),(\-?\d+(\.\d+)?),(\-?\d+(\.\d+)?),(\-?\d+(\.\d+)?),(\-?\d+(\.\d+)?)
6:%R1P,0,0:(\d+),(\-?\d+(\.\d+)?),(\-?\d+(\.\d+)?),(\-?\d+(\.\d+)?),(\-?\d+(\.\d+)?)
7:%R1P,0,0:(\d+),(\-?\d+(\.\d+)?),(\-?\d+(\.\d+)?),(\-?\d+(\.\d+)?),(\-?\d+(\.\d+)?)
8:%R1P,0,0:(\d+),(\-?\d+(\.\d+)?),(\-?\d+(\.\d+)?),(\-?\d+(\.\d+)?),(\-?\d+(\.\d+)?)
9:%R1P,0,0:(\d+),(\-?\d+(\.\d+)?),(\-?\d+(\.\d+)?),(\-?\d+(\.\d+)?),(\-?\d+(\.\d+)?)
由于输入中未包含小数,故3、5、7、9均为空字符串,在其它平台如java等为null,Match.Groups是一个集合,你也可以遍历各分组的值。
0:%R1P,0,0:(\d+),(\-?\d+(\.\d+)?),(\-?\d+(\.\d+)?),(\-?\d+(\.\d+)?),(\-?\d+(\.\d+)?)
1:%R1P,0,0:(\d+),(\-?\d+(\.\d+)?),(\-?\d+(\.\d+)?),(\-?\d+(\.\d+)?),(\-?\d+(\.\d+)?)
2:%R1P,0,0:(\d+),(\-?\d+(\.\d+)?),(\-?\d+(\.\d+)?),(\-?\d+(\.\d+)?),(\-?\d+(\.\d+)?)
3:%R1P,0,0:(\d+),(\-?\d+(\.\d+)?),(\-?\d+(\.\d+)?),(\-?\d+(\.\d+)?),(\-?\d+(\.\d+)?)
4:%R1P,0,0:(\d+),(\-?\d+(\.\d+)?),(\-?\d+(\.\d+)?),(\-?\d+(\.\d+)?),(\-?\d+(\.\d+)?)
5:%R1P,0,0:(\d+),(\-?\d+(\.\d+)?),(\-?\d+(\.\d+)?),(\-?\d+(\.\d+)?),(\-?\d+(\.\d+)?)
6:%R1P,0,0:(\d+),(\-?\d+(\.\d+)?),(\-?\d+(\.\d+)?),(\-?\d+(\.\d+)?),(\-?\d+(\.\d+)?)
7:%R1P,0,0:(\d+),(\-?\d+(\.\d+)?),(\-?\d+(\.\d+)?),(\-?\d+(\.\d+)?),(\-?\d+(\.\d+)?)
8:%R1P,0,0:(\d+),(\-?\d+(\.\d+)?),(\-?\d+(\.\d+)?),(\-?\d+(\.\d+)?),(\-?\d+(\.\d+)?)
9:%R1P,0,0:(\d+),(\-?\d+(\.\d+)?),(\-?\d+(\.\d+)?),(\-?\d+(\.\d+)?),(\-?\d+(\.\d+)?)
由于输入中未包含小数,故3、5、7、9均为空字符串,在其它平台如java等为null,Match.Groups是一个集合,你也可以遍历各分组的值。
解决方案:25分
给组命名就行了
var m = Regex.Match("XXXXX","(这里是一个组)(?<test>这里又是一个组)") m.Groups[1].Value = "这里是一个组" m.Groups[2].Value = m.Groups["test"] = “这里又是一个组” (?<名字>XXX) 、//在括号内开头?<> 可以给组命名