在正则表达式中使用引号,在 C# 中使用引号 [英] use quotation marks in regex, in quotation marks in C#

查看:81
本文介绍了在正则表达式中使用引号,在 C# 中使用引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:本题中所有的引号实际上都是代码的一部分.

Note: all the quotation marks in this question are actually part of the code.

我正在学习正则表达式,我正在尝试抓取一个带有音乐的网站.我将站点的源代码放入一个名为ytcmusic.txt"的文本文件中.这是 html 的示例:

I'm learning regex, and i'm trying to scrape a site with music on it. I put the source of the site into a text file called 'ytcmusic.txt'. Here's a sample of the html:

<li><a href="angelpool%20-%20know.mp3"> angelpool - know.mp3</a></li>
<li><a href="angelpool%20-%20sellout.mp3"> angelpool - sellout.mp3</a></li>
<li><a href="angelpool%20-%20time.mp3"> angelpool - time.mp3</a></li>
<li><a href="bella%20-%20gibsons.mp3"> bella - gibsons.mp3</a></li>

我将使用第一行作为示例,我试图只抓取angelpool%20-%20know.mp3",为此我使用了正则表达式:.*.mp3"——---- 放到C#中的时候,必须用引号括起来,这样就破坏了正则表达式中的引号.代码如下(它不会编译,如果您删除正则表达式周围的一组引号,它会但显然不会返回源的正确部分):

i'll use the first line as an example, i'm trying to scrape only the "angelpool%20-%20know.mp3" and to do that here's the regex i used: ".*.mp3" ------ when I put it into C#, I have to surround it in quotation marks, which ruins the quotation marks in the regex. heres the code (it doesn't compile, if you remove one set of quotation marks around the regex, it does but obviously doesnt return the correct part of the source):

var sr = new StreamReader("ytcmusic.txt");
        string str = sr.ReadToEnd();
        var match = Regex.Match(str, @".*.mp3");

提前致谢!

推荐答案

这就行了

"[^"]*"

请注意,我将保留您的示例输入并假设标题是唯一引用的内容.如果不是这种情况,您必须在正则表达式中添加更多上下文.

Note that I'm keeping you to your sample input and assuming the titles are the only thing quoted. If that's not the case you have to put more context into the regex.

如果你想在没有引号的情况下捕获,你可以像这样引入括号

If you want to capture without the quotes you can introduce parenthesis like so

"([^"]*)"

在 C# 中这变成了

StringCollection resultList = new StringCollection();
Regex regexObj = new Regex("\"([^\"]*)\"");
Match matchResult = regexObj.Match(subjectString);
while (matchResult.Success) {
    resultList.Add(matchResult.Groups[1].Value);
    matchResult = matchResult.NextMatch();
} 

这篇关于在正则表达式中使用引号,在 C# 中使用引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆