C# 如何在一个foreach中匹配两个字符串?

C# 如何在一个foreach中匹配两个字符串?,c#,regex,foreach,C#,Regex,Foreach,我使用正则表达式来匹配文件中的字符,但我想匹配该文件中的两个不同字符串,但它们出现了不止一次,这就是我使用循环的原因。我可以匹配单个字符串,但不能匹配两个字符串 Regex celcius = new Regex(@"""temp"":\d*\.?\d{1,3}"); foreach (Match match in celcius.Matches(htmlcode)) { Regex date = new Regex(@"\d{4}-\d{2}-\d{2}"); foreac

我使用正则表达式来匹配文件中的字符,但我想匹配该文件中的两个不同字符串,但它们出现了不止一次,这就是我使用循环的原因。我可以匹配单个字符串,但不能匹配两个字符串

Regex celcius = new Regex(@"""temp"":\d*\.?\d{1,3}"); 
foreach (Match match in celcius.Matches(htmlcode))
{ 
    Regex date = new Regex(@"\d{4}-\d{2}-\d{2}");
    foreach (Match match1 in date.Matches(htmlcode))
    {
        string date1 = Convert.ToString(match1.Value);
        string temperature = Convert.ToString(match.Value);
        Console.Write(temperature + "\t" + date1);
    }
}
htmlcode:

{"temp":287.05,"temp_min":286.932,"temp_max":287.05,"pressure":1019.04,"sea_level":1019.04,"grnd_level":1001.11,"humidity":89,"temp_kf":0.12},"weather":[{"id":804,"main":"Clouds","description":"overcast
clouds","icon":"04n"}],"clouds":{"all":100},"wind":{"speed":0.71,"deg":205.913},"sys":{"pod":"n"},"dt_txt":"2019-09-22
21:00:00"},{"dt":1569196800,"main":{"temp":286.22,"temp_min":286.14,"temp_max":286.22,"pressure":1019.27,"sea_level":1019.27,"grnd_level":1001.49,"humidity":90,"temp_kf":0.08},"weather":[{"id":804,"main":"Clouds","description":"overcast
clouds","icon":"04n"}],"clouds":{"all":99},"wind":{"speed":0.19,"deg":31.065},"sys":{"pod":"n"},"dt_txt":"2019-09-23
00:00:00"},{"dt":1569207600,"main":{"temp":286.04,"temp_min":286,"temp_max":286.04,"pressure":1019.38,"sea_level":1019.38,"grnd_level":1001.03,"humidity":89,"temp_kf":0.04},"weather":
string htmlcode = // ...
var matches = Regex.Matches(htmlcode, @"(""temp"":\d*\.?\d{1,3}).*?(\d{4}-\d{2}-\d{2})");
foreach (Match m in matches)
{
    Console.WriteLine(m.Groups[1].Value + "\t" + m.Groups[2].Value);
}

您可以使用一个Regex模式和两个温度和日期捕获组。该模式可以如下所示:

(“temp”:\d*\。?\d{1,3})。*?(\d{4}-\d{2}-\d{2})

C#示例:

{"temp":287.05,"temp_min":286.932,"temp_max":287.05,"pressure":1019.04,"sea_level":1019.04,"grnd_level":1001.11,"humidity":89,"temp_kf":0.12},"weather":[{"id":804,"main":"Clouds","description":"overcast
clouds","icon":"04n"}],"clouds":{"all":100},"wind":{"speed":0.71,"deg":205.913},"sys":{"pod":"n"},"dt_txt":"2019-09-22
21:00:00"},{"dt":1569196800,"main":{"temp":286.22,"temp_min":286.14,"temp_max":286.22,"pressure":1019.27,"sea_level":1019.27,"grnd_level":1001.49,"humidity":90,"temp_kf":0.08},"weather":[{"id":804,"main":"Clouds","description":"overcast
clouds","icon":"04n"}],"clouds":{"all":99},"wind":{"speed":0.19,"deg":31.065},"sys":{"pod":"n"},"dt_txt":"2019-09-23
00:00:00"},{"dt":1569207600,"main":{"temp":286.04,"temp_min":286,"temp_max":286.04,"pressure":1019.38,"sea_level":1019.38,"grnd_level":1001.03,"humidity":89,"temp_kf":0.04},"weather":
string htmlcode = // ...
var matches = Regex.Matches(htmlcode, @"(""temp"":\d*\.?\d{1,3}).*?(\d{4}-\d{2}-\d{2})");
foreach (Match m in matches)
{
    Console.WriteLine(m.Groups[1].Value + "\t" + m.Groups[2].Value);
}
输出:

{"temp":287.05,"temp_min":286.932,"temp_max":287.05,"pressure":1019.04,"sea_level":1019.04,"grnd_level":1001.11,"humidity":89,"temp_kf":0.12},"weather":[{"id":804,"main":"Clouds","description":"overcast
clouds","icon":"04n"}],"clouds":{"all":100},"wind":{"speed":0.71,"deg":205.913},"sys":{"pod":"n"},"dt_txt":"2019-09-22
21:00:00"},{"dt":1569196800,"main":{"temp":286.22,"temp_min":286.14,"temp_max":286.22,"pressure":1019.27,"sea_level":1019.27,"grnd_level":1001.49,"humidity":90,"temp_kf":0.08},"weather":[{"id":804,"main":"Clouds","description":"overcast
clouds","icon":"04n"}],"clouds":{"all":99},"wind":{"speed":0.19,"deg":31.065},"sys":{"pod":"n"},"dt_txt":"2019-09-23
00:00:00"},{"dt":1569207600,"main":{"temp":286.04,"temp_min":286,"temp_max":286.04,"pressure":1019.38,"sea_level":1019.38,"grnd_level":1001.03,"humidity":89,"temp_kf":0.04},"weather":
string htmlcode = // ...
var matches = Regex.Matches(htmlcode, @"(""temp"":\d*\.?\d{1,3}).*?(\d{4}-\d{2}-\d{2})");
foreach (Match m in matches)
{
    Console.WriteLine(m.Groups[1].Value + "\t" + m.Groups[2].Value);
}
“温度”:287.05 2019-09-22
“温度”:286.22 2019-09-23

.

我想你没有HTML。我认为您有一个名为JSON(JavaScript对象通知)的集合,这是一种高效传递数据的方法

因此,这是一个“HTML”对象

因此,我建议使用C#web助手转换该行并直接解析对象

//include this library
using System.Web.Helpers;

//parse your htmlcode using this loop
foreach(var line in htmlcode)
{
    dynamic data = JSON.decode(line);
    string temperature = (string)data["temp"];
    string date = Convert.ToDateTime(data["dt_txt"]).ToString("yyyy-MM-dd");
    Console.WriteLine($"temperature: {temperature} date: {date}"");
}

在正则表达式中使用OR(|)?请提供一个输入文本的示例
htmlcode
@“temp”“:\d*\。?\d{1,3}”
看起来您是在用正则表达式解析JSON如果for循环更适合这个问题,那么最好使用for循环。