Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用c在文本文件中搜索模式#_C#_Regex_Pattern Matching_Text Files - Fatal编程技术网

C# 使用c在文本文件中搜索模式#

C# 使用c在文本文件中搜索模式#,c#,regex,pattern-matching,text-files,C#,Regex,Pattern Matching,Text Files,我有多个文本文件。我需要下面代码中的值'UA-8798837-1'(不带qoutes)。 因此,步骤将是-: 打开文本文件abc.txt 从“\u gaq.push”开始搜索模式。 如果模式匹配,我将跳过几个字符“([''u setAccount',” 选择键“UA-8798837-1”(不带引号) 将此键值保存在名为“key”的变量中 文本文件的代码-: <script type="text/javascript">//<![CDATA[ var _gaq = _ga

我有多个文本文件。我需要下面代码中的值
'UA-8798837-1'
(不带qoutes)。 因此,步骤将是-:

  • 打开文本文件abc.txt
  • “\u gaq.push”开始搜索模式。
  • 如果模式匹配,我将跳过几个字符
    “([''u setAccount',”
  • 选择键“UA-8798837-1”(不带引号)
  • 将此键值保存在名为“key”的变量中
  • 文本文件的代码-:

    <script type="text/javascript">//<![CDATA[
        var _gaq = _gaq || [];
        _gaq.push(['_setAccount', 'UA-8798837-1']);
    <script>
    
    //
    
    所以基本上我需要搜索一个模式,如果找到了,我需要跳过几个字符来选择字符“'”后面的代码,并复制这个值,直到再次找到字符“'”。 选择从字符
    “'”
    “UA-8798837-1”开始,并在字符
    “'”结束。
    值为UA-8798837-1。
    请告诉我怎么做。提前谢谢。

    这是一个非常简单的正则表达式:

    /\[\''u setAccount\',\'(.+)\'/

    您可以看到匹配组的外观


    如果您以前没有在C#中使用过正则表达式,那么这是一个很好的开始。因为您希望使用多个匹配项,所以可能更相关。

    我想您正在寻找此正则表达式:

    (?<=_gaq\.push\(\['_setAccount',\s')(.*)(?=')
    

    (?我相信所有谷歌分析代码都是以“UA-”开头的。如果这是真的,你也许可以简化搜索,查找该文本并抓取所有内容,直到出现
    character。但是如何选择该值UA-8798837-1 c#code请。我不会为您编写程序。我已经提供了可以帮助您解决问题的链接。如果您对您遇到的问题有具体问题,请返回,我怀疑这里是否有人愿意为您做任何事情。