C# 正则表达式解析Int和String
所以我创建了这个正则表达式来解析如下字符串(我需要字节和时间的值): 这是下面的代码(已使用) 当字符串表示为一行时,这种方法非常有效,但是当我尝试读取文件时,就像这样C# 正则表达式解析Int和String,c#,regex,C#,Regex,所以我创建了这个正则表达式来解析如下字符串(我需要字节和时间的值): 这是下面的代码(已使用) 当字符串表示为一行时,这种方法非常有效,但是当我尝试读取文件时,就像这样 1463735418 Bytes: 0 Time: 4.297 1463735424 Time: 2.205 1466413696 Time: 2.225 1466413699 1466413702 1466413705 1466413708 1466413711 1466413714 1466413717 1466413720
1463735418
Bytes: 0
Time: 4.297
1463735424
Time: 2.205
1466413696
Time: 2.225
1466413699
1466413702
1466413705
1466413708
1466413711
1466413714
1466413717
1466413720
Bytes: 7037
Time: 59.320
... (arbritrary repition)
我得到垃圾数据
Expected Output:
0, 4.297
7037, 59.320
(仅当存在时间字节对时匹配)
编辑:我正在尝试这样的事情,但仍然没有得到想要的结果
foreach (string txt in lines)
{
if (txt.StartsWith("Byte"))
{
string re1 = ".*?"; // Non-greedy match on filler
string re2 = "(\\d+)"; // Integer Number 1
Regex r = new Regex(re1 + re2, RegexOptions.IgnoreCase | RegexOptions.Singleline);
Match m = r.Match(txt);
if (m.Success)
{
String int1 = m.Groups[1].ToString();
//Console.Write("(" + int1.ToString() + ")" + "\n");
httpTable += int1.ToString() + ",";
}
}
if (txt.StartsWith("Time"))
{
string re3 = ".*?"; // Non-greedy match on filler
string re4 = "([+-]?\\d*\\.\\d+)(?![-+0-9\\.])"; // Float 1
Regex r1 = new Regex(re3 + re4, RegexOptions.IgnoreCase | RegexOptions.Singleline);
Match m1 = r1.Match(txt);
if (m1.Success)
{
String float1 = m1.Groups[1].ToString();
//Console.Write("(" + float1.ToString() + ")" + "\n");
httpTable += float1.ToString() + Environment.NewLine;
}
}
}
我怎样才能修理它?
谢谢。因为您只需要
字节:…
和时间:…
的值,所以请使用精确的字符串,而不是填充符:
用于捕获字节
用于捕获时间
捕获两者的通用模式
我建议使用lookback限定时间和字节,如果找不到,则默认为整数类别。然后,通过使用regex命名的捕获来确定每个匹配的结果
string data = "1463735418 Bytes: 0 Time: 4.297 1463735424 Time: 2.205 1466413696 Time: 2.225 1466413699 1466413702 1466413705 1466413708 1466413711 1466413714 1466413717 1466413720 Bytes: 7037 Time: 59.320";
string pattern = @"
(?<=Bytes:\s)(?<Bytes>\d+) # Lookbehind for the bytes
| # Or
(?<=Time:\s)(?<Time>[\d.]+) # Lookbehind for time
| # Or
(?<Integer>\d+) # most likely its just an integer.
";
Regex.Matches(data, pattern, RegexOptions.IgnorePatternWhitespace)
.OfType<Match>()
.Select(mt => new
{
IsInteger = mt.Groups["Integer"].Success,
IsTime = mt.Groups["Time"].Success,
IsByte = mt.Groups["Bytes"].Success,
strMatch = mt.Groups[0].Value,
AsInt = mt.Groups["Integer"].Success ? int.Parse(mt.Groups["Integer"].Value) : -1,
AsByte = mt.Groups["Bytes"].Success ? int.Parse(mt.Groups["Bytes"].Value) : -1,
AsTime = mt.Groups["Time"].Success ? double.Parse(mt.Groups["Time"].Value) : -1.0,
})
string data=“1463735418字节:0时间:4.297 1463735424时间:2.205 1466413696时间:2.225 1466413699 1466413702 1466413705 1466413708 1466413711 1466413714字节:7037时间:59.320”;
字符串模式=@“
(?此输入不符合现有模式。此输入的预期输出是什么?@WiktorStribiżew添加了预期输出使用Harry Potter的想法,由于字节在时间之前出现,因此在找到整对字节后使用标志重置。或者,如果使用.ReadToEnd()读取文件内容
您可以使用。这取决于文件的大小。谢谢。我在问题中添加了预期的输出,很抱歉以前忘记添加。泛型模式与它匹配吗?如果不向代码添加更多逻辑,这将不起作用,因为当前文件被读取到一个行数组中。@WiktorStribiżew我添加了一些逻辑并更新了que尽管如此,我仍然无法构造一个逻辑来保留字节更少的时间条目。您不需要在这里使用lookbehind。(?@WiktorStribiżew good think…我想安全行事,以防OP没有提到某些情况。这是一些复杂的代码!非常感谢。我如何访问mt
来编写我的文件?我对LINQ
真的很糟糕,谢谢。@jeet你没有访问mt
,因为选择
已经投射了它,我在控制台应用程序中调试程序,并在分配var-theResult=Regex.Matches…
@jeet One不必使用我在示例中显示为结果的IEnumerable
,如果Linq不是他们的特长,可以使用for循环遍历匹配。我只是想通过将结果放入动态实体中查看来显示结果的所有排列。当您说“打印”时,如果您指的是调试并使用它,我将使用升级的intellsense,这就是打印屏幕示例的来源。
foreach (string txt in lines)
{
if (txt.StartsWith("Byte"))
{
string re1 = ".*?"; // Non-greedy match on filler
string re2 = "(\\d+)"; // Integer Number 1
Regex r = new Regex(re1 + re2, RegexOptions.IgnoreCase | RegexOptions.Singleline);
Match m = r.Match(txt);
if (m.Success)
{
String int1 = m.Groups[1].ToString();
//Console.Write("(" + int1.ToString() + ")" + "\n");
httpTable += int1.ToString() + ",";
}
}
if (txt.StartsWith("Time"))
{
string re3 = ".*?"; // Non-greedy match on filler
string re4 = "([+-]?\\d*\\.\\d+)(?![-+0-9\\.])"; // Float 1
Regex r1 = new Regex(re3 + re4, RegexOptions.IgnoreCase | RegexOptions.Singleline);
Match m1 = r1.Match(txt);
if (m1.Success)
{
String float1 = m1.Groups[1].ToString();
//Console.Write("(" + float1.ToString() + ")" + "\n");
httpTable += float1.ToString() + Environment.NewLine;
}
}
}
Bytes: (\d+)
Time: ([-+]\d*\.\d+)
(Bytes|Time): (\d+|[-+]\d*\.\d+)
string data = "1463735418 Bytes: 0 Time: 4.297 1463735424 Time: 2.205 1466413696 Time: 2.225 1466413699 1466413702 1466413705 1466413708 1466413711 1466413714 1466413717 1466413720 Bytes: 7037 Time: 59.320";
string pattern = @"
(?<=Bytes:\s)(?<Bytes>\d+) # Lookbehind for the bytes
| # Or
(?<=Time:\s)(?<Time>[\d.]+) # Lookbehind for time
| # Or
(?<Integer>\d+) # most likely its just an integer.
";
Regex.Matches(data, pattern, RegexOptions.IgnorePatternWhitespace)
.OfType<Match>()
.Select(mt => new
{
IsInteger = mt.Groups["Integer"].Success,
IsTime = mt.Groups["Time"].Success,
IsByte = mt.Groups["Bytes"].Success,
strMatch = mt.Groups[0].Value,
AsInt = mt.Groups["Integer"].Success ? int.Parse(mt.Groups["Integer"].Value) : -1,
AsByte = mt.Groups["Bytes"].Success ? int.Parse(mt.Groups["Bytes"].Value) : -1,
AsTime = mt.Groups["Time"].Success ? double.Parse(mt.Groups["Time"].Value) : -1.0,
})