C# 在C中的字符串中查找字符串#

C# 在C中的字符串中查找字符串#,c#,C#,我有一个字符串,更具体地说,它是一段HTML源代码,但由于某些原因,我需要的行都写为一行(因此基本上,使用ReadLine()是一个字符串)。现在在这个字符串中,我需要提取某个单词 下面是一段提取的html代码。我需要的是/Qur%27an/Luhaidan/001.mp3部分,用于每个.mp3文件,最多114.mp3 ...<th scope="colgroup"><a href="/Qur%27an/Luhaidan/001.mp3"><img src="...

我有一个字符串,更具体地说,它是一段HTML源代码,但由于某些原因,我需要的行都写为一行(因此基本上,使用ReadLine()是一个字符串)。现在在这个字符串中,我需要提取某个单词

下面是一段提取的html代码。我需要的是
/Qur%27an/Luhaidan/001.mp3
部分,用于每个.mp3文件,最多114.mp3

...<th scope="colgroup"><a href="/Qur%27an/Luhaidan/001.mp3"><img src="...
...<th scope="colgroup"><a href="/Qur%27an/Luhaidan/002.mp3"><img src="...
...<th scope="colgroup"><a href="/Qur%27an/Luhaidan/114.mp3"><img src="...

…虽然一般来说,阅读HTML的最佳方法是使用HTML解析器,但像这样的简单任务可以用正则表达式来处理

这样的表达应该有效:

href="(.*?[.]mp3)"
在循环中搜索此正则表达式,并提取文件名的第一个组

var str = @"
...<th scope=""colgroup""><a href=""/Qur%27an/Luhaidan/001.mp3""><img src=""...
...<th scope=""colgroup""><a href=""/Qur%27an/Luhaidan/002.mp3""><img src=""...
...<th scope=""colgroup""><a href=""/Qur%27an/Luhaidan/114.mp3""><img src=""...
";
foreach (Match m in Regex.Matches(str, "href=\"(.*?[.]mp3)\"")) {
    Console.WriteLine(m.Groups[1]);
}

虽然一般来说,阅读HTML的最佳方法是使用HTML解析器,但像这样的简单任务可以用正则表达式来处理

这样的表达应该有效:

href="(.*?[.]mp3)"
在循环中搜索此正则表达式,并提取文件名的第一个组

var str = @"
...<th scope=""colgroup""><a href=""/Qur%27an/Luhaidan/001.mp3""><img src=""...
...<th scope=""colgroup""><a href=""/Qur%27an/Luhaidan/002.mp3""><img src=""...
...<th scope=""colgroup""><a href=""/Qur%27an/Luhaidan/114.mp3""><img src=""...
";
foreach (Match m in Regex.Matches(str, "href=\"(.*?[.]mp3)\"")) {
    Console.WriteLine(m.Groups[1]);
}

使用此方法,您可以从一个长字符串中获取一个值:

String input = @"...<th scope=""colgroup""><a href=""/Qur%27an/Luhaidan/001.mp3""><img src=""...
            ...<th scope=""colgroup""><a href=""/Qur%27an/Luhaidan/002.mp3""><img src=""...
            ...<th scope=""colgroup""><a href=""/Qur%27an/Luhaidan/114.mp3""><img src=""...";

foreach (Match match in Regex.Matches(input, @"href\=\""(.*?\.mp3)"))
{
    String yourvalue = match.Value;
}
字符串输入=@“。。。

您需要将与提取的值相关的代码放置在
yourvalue的位置。

您可以使用以下方法从一个long中获取值:

String input = @"...<th scope=""colgroup""><a href=""/Qur%27an/Luhaidan/001.mp3""><img src=""...
            ...<th scope=""colgroup""><a href=""/Qur%27an/Luhaidan/002.mp3""><img src=""...
            ...<th scope=""colgroup""><a href=""/Qur%27an/Luhaidan/114.mp3""><img src=""...";

foreach (Match match in Regex.Matches(input, @"href\=\""(.*?\.mp3)"))
{
    String yourvalue = match.Value;
}
字符串输入=@“。。。

代替
yourvalue
您需要放置与提取的值相关的代码。

考虑以下代码片段来提取mp3文件名

var matches = Regex.Matches(inputMessage, @"(?<=\"")[\w\s\d/%]*?\.mp3");

var matches=Regex.matches(inputMessage,@)(?考虑以下代码片段来提取mp3文件名

var matches = Regex.Matches(inputMessage, @"(?<=\"")[\w\s\d/%]*?\.mp3");

var matches=Regex.matches(inputMessage,@)(?当我尝试使用这个richTextBox3.Text=m.Groups[1]时,我得到一个错误:无法将type System.Text.RegularExpressions.Group'隐式转换为'string'@FJam您可以添加
.Value
(即make
m.Groups[1].Value
)要将组转换为
字符串
。非常感谢我在尝试使用此richTextBox3.Text=m.Groups[1]时得到了它;我收到一个错误,错误是:无法将type System.Text.RegularExpressions.group'隐式转换为'string'@FJam您可以添加
.Value
(即,make
m.group[1].Value
)将组转换为
字符串
。非常感谢,我收到了