C#和Regex:如何在引号之间提取字符串 [英] C# and Regex: How to extract strings between quotation marks

查看:291
本文介绍了C#和Regex:如何在引号之间提取字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下字符串:


<script language="javascript">
        var league = new Array(
            "Soccer","Germany - 2. Bundesliga","38542195","102","24 May 2009 14:00","24 May 2009 14:00","1X2","1","0"
        );
        var matches = new Array(
            "125","1.FC Nurnberg - TSV 1860 Munich","24 May 2009 14:00","Sun, 24.05.09 14:00","1|1.40|4.10|6.40|-","||||","1|1.90|3.50|2.20|0:1","1|1.05|2.20|1.18|-","1|2.00||1.60|2.5","1|3.40|3.20|1.60|2","1|1.70|2.50|5.50|-","||||-","1",
            "126","FC Ingolstadt 04 - TuS Koblenz","24 May 2009 14:00","Sun, 24.05.09 14:00","1|3.60|2.80|2.00|-","||||","||||:","1|1.68|1.25|1.26|-","1|1.90||1.70|2.5","1|3.10|3.10|1.70|2","1|3.60|2.10|2.45|-","||||-","1",
            "127","FC St.Pauli 1910 - FSV Frankfurt","24 May 2009 14:00","Sun, 24.05.09 14:00","1|2.50|2.95|2.60|-","||||","||||:","1|1.41|1.44|1.28|-","1|2.00||1.60|2.5","1|3.40|3.20|1.60|2","1|2.95|2.00|3.05|-","||||-","1",
            "128","MSV Duisburg - VfL Osnabruck","24 May 2009 14:00","Sun, 24.05.09 14:00","1|2.30|3.60|2.40|-","||||","||||:","1|1.35|1.51|1.27|-","1|2.10||1.55|2.5","1|3.60|3.20|1.55|2","||||-","||||-","1",
            "129","FSV Mainz 05 - SC Rot-Weiss Oberhausen","24 May 2009 14:00","Sun, 24.05.09 14:00","1|1.40|3.80|7.00|-","||||","1|1.95|3.50|2.50|0:1","1|1.05|2.50|1.18|-","1|2.00||1.60|2.5","1|3.40|3.20|1.60|2","1|1.70|2.30|5.50|-","||||-","1",
            "130","Rot-Weiss Ahlen - SpVgg Greuther Furth","24 May 2009 14:00","Sun, 24.05.09 14:00","1|2.55|3.20|2.55|-","||||","||||:","1|1.42|1.42|1.28|-","1|2.10||1.55|2.5","1|3.60|3.20|1.55|2","1|3.00|2.00|3.00|-","||||-","1",
            "131","SC Freiburg - 1.FC Kaiserslautern","24 May 2009 14:00","Sun, 24.05.09 14:00","1|1.75|3.25|4.20|-","||||","||||:","1|1.17|1.91|1.24|-","1|2.10||1.55|2.5","1|3.60|3.20|1.55|2","1|2.30|2.10|3.80|-","||||-","1",
            "132","SV Wehen Wiesbaden - FC Hansa Rostock","24 May 2009 14:00","Sun, 24.05.09 14:00","1|5.00|3.70|1.55|-","||||","||||:","1|2.23|1.09|1.23|-","1|1.90||1.70|2.5","1|3.10|3.10|1.70|2","1|4.50|2.25|2.00|-","||||-","1",
            "133","TSV Alemannia Aachen - FC Augsburg","24 May 2009 14:00","Sun, 24.05.09 14:00","1|1.60|3.45|5.10|-","||||","||||:","1|1.11|2.13|1.23|-","1|2.10||1.55|2.5","1|3.60|3.20|1.55|2","1|2.10|2.20|4.30|-","||||-","1"
        );
        var events = showLeague(league, matches);
        hasEvents = hasEvents + events;
</script>

我想做的是解析它读取"var match"的部分,并提取两个引号之间包含的所有内容.因此,所需的结果应该是包含以下内容的数组:

What I'm trying to do is to parse the part where it reads "var matches" and extract anything contained between two quotation marks. Therefore the desired result should be an array containing:


(0): 125
(1): 1.FC Nurnberg - TSV 1860 Munich
(2): 24 May 2009 14:00 
etc.

注意:我看到一个类似的问题正在回答,但是花了一段时间后,我还是无法解决它.谢谢!

推荐答案

请不要为此使用正则表达式,CSV应该由解析器处理.使用正则表达式执行此操作是所有方法中最慢且最容易出错的方法.

Please don't use regular expressions for this, CSV should be handled by a parser. Doing this with regex is the slowest and most error-prone method of all.

这是一个现成的解析器: codeproject.com:一种快速CSV读者.可以轻松找到其他示例,因为实现CSV解析器是一种很常见的练习.

Here is a ready-to-use parser: codeproject.com: A Fast CSV Reader. Others examples can easily be found, as implementing a CSV parser is a popular excercise.

您还可以使用OLEDB内置解析器: C#教程-使用内置的OLEDB CSV分析器.

You can also use the OLEDB built-in parser: C# Tutorial - Using The Built In OLEDB CSV Parser.

在您的示例中,我将使用IndexOf()剪切"var matches = new Array("");"之间的字符串,并将结果视为CSV字符串.

With your example, I would use IndexOf() to cut out the string between "var matches = new Array(" and ");", and treat the result as a CSV string.

这篇关于C#和Regex:如何在引号之间提取字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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