早教吧作业答案频道 -->其他-->
C#中Regex的一个小问题想要将目标代码中第一次出现以下标签中间内的值matches给我自定义的变量.0114025031——————————————我使用以下代码,请问newRegex的括号里,怎么写?RegexmyR
题目详情
C#中Regex的一个小问题
想要将目标代码中第一次出现以下标签中间内的值matches给我自定义的变量.
0114
025
031
——————————————
我使用以下代码,请问new Regex的括号里,怎么写?
Regex myRegex = new Regex();
MatchCollection matches = myRegex.Matches(inputString);
redMiss1.Text = matches[0].Groups[1].Value.ToString();
redMiss2.Text = matches[1].Groups[1].Value.ToString();
redMiss3.Text = matches[2].Groups[1].Value.ToString();
想要将目标代码中第一次出现以下标签中间内的值matches给我自定义的变量.
0114
025
031
——————————————
我使用以下代码,请问new Regex的括号里,怎么写?
Regex myRegex = new Regex();
MatchCollection matches = myRegex.Matches(inputString);
redMiss1.Text = matches[0].Groups[1].Value.ToString();
redMiss2.Text = matches[1].Groups[1].Value.ToString();
redMiss3.Text = matches[2].Groups[1].Value.ToString();
▼优质解答
答案和解析
这个应该是标准答案,与上面的的正则表达式,有一个区别就是加了?非贪婪模式搜索
private void GetRegexValue()
{
//定义多个ul
string xml = @"
0114
025
031
0115
026
032
";
//第一个正则表达式,获取ul,与上一个区别在于加了?非贪婪模式搜索
string pattern = "[\\s\\S]+?";
MatchCollection mc = Regex.Matches(xml, pattern, RegexOptions.IgnoreCase);
if (mc.Count > 0)
{
string matchStr = mc[0].Value;
//第二个表达式,获取所有em值
MatchCollection matches = Regex.Matches(matchStr, "(?\\d+)");
string outStr = "";
if (matches.Count > 0)
{
//组织返回值,可以按照你的代码进行修改
foreach (Match match in matches)
{
outStr += match.Groups["v"].Value + ";";
}
//可用下面的代码替换
//redMiss1.Text = matches[0].Groups[1].Value.ToString();
//redMiss2.Text = matches[1].Groups[1].Value.ToString();
//redMiss3.Text = matches[2].Groups[1].Value.ToString();
}
}
}
private void GetRegexValue()
{
//定义多个ul
string xml = @"
0114
025
031
0115
026
032
";
//第一个正则表达式,获取ul,与上一个区别在于加了?非贪婪模式搜索
string pattern = "[\\s\\S]+?";
MatchCollection mc = Regex.Matches(xml, pattern, RegexOptions.IgnoreCase);
if (mc.Count > 0)
{
string matchStr = mc[0].Value;
//第二个表达式,获取所有em值
MatchCollection matches = Regex.Matches(matchStr, "(?\\d+)");
string outStr = "";
if (matches.Count > 0)
{
//组织返回值,可以按照你的代码进行修改
foreach (Match match in matches)
{
outStr += match.Groups["v"].Value + ";";
}
//可用下面的代码替换
//redMiss1.Text = matches[0].Groups[1].Value.ToString();
//redMiss2.Text = matches[1].Groups[1].Value.ToString();
//redMiss3.Text = matches[2].Groups[1].Value.ToString();
}
}
}
看了 C#中Regex的一个小问题...的网友还看了以下:
298K下,将1mol蔗糖溶解在1L水中,此溶解过程中体系的△G=△H-T△S和△S的变化情况是( 2020-05-17 …
阅读下列算法,指出算法A的功能和时间复杂度,其中h、g分别为单循环链表中两个节点指针.voidB( 2020-06-12 …
ABCD是有同种材料制成的四个圆柱体高度相同G不同,比较p=F/S=G/S=mg/(V/h)=mg 2020-06-26 …
ABCD是有同种材料制成的四个圆柱体,高度相同G不同p=F/S=G/S=mg/(V/h)=mgh/ 2020-06-26 …
已知传递函数 G(S)=6s2+1/s3+3s2+3s=1 H(S)=(s+1)(s+2)/(s+ 2020-06-27 …
反应CH3OH(l)+NH3(g)═CH3NH2(g)+H2O(g)在某温度自发向右进行,若反应| 2020-07-12 …
(1)工业清洗硅片上SiO2(s)的反应是:SiO2(s)+4HF(g)═SiF4(g)+2H2O 2020-07-22 …
下列算法,指出算法A的功能和时间复杂度,其中h、g分别为单循环链表中两个节点指针.VoidB(in 2020-07-23 …
某反应2AB(g)⇌C(g)+3D(g)在高温时能自发进行,其逆反应在低温下能自发进行,则该反应的△ 2020-11-01 …
若化学计量数相同,当反应物、生成物状态不同时,为何△H的值不同.解释下S(s)+O2(g)=SO2( 2020-12-31 …