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#_Regex - Fatal编程技术网

C# 雷杰克斯:这是什么意思。后跟+?

C# 雷杰克斯:这是什么意思。后跟+?,c#,regex,C#,Regex,很抱歉问这个问题,但我真的被卡住了。此代码属于已离开公司的人。这会引起问题 protected override string CleanDataLine(string line) { //the regular expression for GlobalSight log Regex regex = new Regex("\".+\""); Match match = regex.Match(line); if (matc

很抱歉问这个问题,但我真的被卡住了。此代码属于已离开公司的人。这会引起问题

protected override string CleanDataLine(string line)
    {
        //the regular expression for GlobalSight log
        Regex regex = new Regex("\".+\"");
        Match match = regex.Match(line);
        if (match.Success)
        {
            string matchPart = match.Value;
            matchPart = 
                  matchPart.Replace(string.Format("\"{0}\"", 
                  Delimiter), string.Format("\"{0}\"", "*+*+"));
            matchPart = matchPart.Replace(Delimiter, '_');
            matchPart = 
                  matchPart.Replace(string.Format("\"{0}\"", "*+*+"), 
                  string.Format("\"{0}\"", Delimiter));
            line = line.Replace(match.Value, matchPart);
        }
        return line;
    }
我花了很多时间研究。他想完成什么


谢谢你的帮助

点匹配除换行符以外的任何字符。 +一个或多个等于正则表达式匹配的{1,}

引用, 后跟一个或多个+字符(除换行符外的任何字符),尽可能多, 后面是一段引语。 这不是一个很好的正则表达式。例如,在字符串foo-bar-baz-bam-boom中,它将与bar-baz-bam匹配


如果目的是匹配带引号的字符串,则更合适的正则表达式应该是[^]*。

。是除\n之外的任何字符,+表示1个或多个字符

所以:.+是一个或多个字符

protected override string CleanDataLine(string line)
{
    //the regular expression for GlobalSight log
    Regex regex = new Regex("\".+\"");
    Match match = regex.Match(line);
    if (match.Success)
    {
        string matchPart = match.Value;
        matchPart = 
              matchPart.Replace(string.Format("\"{0}\"", 
              Delimiter), string.Format("\"{0}\"", "*+*+"));
        matchPart = matchPart.Replace(Delimiter, '_');
        matchPart = 
              matchPart.Replace(string.Format("\"{0}\"", "*+*+"), 
              string.Format("\"{0}\"", Delimiter));
        line = line.Replace(match.Value, matchPart);
    }
    return line;
}
行只是一些文本,可以是Hello World,或者其他任何东西

新正则表达式\.+\the\是一个转义引号,这意味着它实际上在寻找一个以双引号开头的字符串..+指一次或多次查找不包括新行字符的任何字符

如果它确实匹配,那么他试图通过获取值来找出匹配的部分


然后它就变成了对匹配字符串的正常搜索和替换。

您需要学习正则表达式的基础知识。我怀疑您是否会花时间进行研究而没有找到答案。相关参考资料:你也可以把一个正则表达式放进去,得到不同组成部分的分类和每个部分的功能解释。他们有简单的正则表达式备忘单——我把它挂在墙上——当你需要了解基本知识时,会让生活变得更轻松。我最喜欢的是。调试它并查看每行上所有变量的值。我还是猜测,我强烈建议调试:如果行中包含一对双引号,请将所有出现的分隔符替换为“不接触双引号中的分隔符”。@AlexeiLevenkov,你在跟踪他吗,还是你把句子末尾的问号错了,表示一个问题是正则表达式的一部分?你一定是正则表达式方面的天才,因为这就是我遇到的问题。事实上,上面的代码在引号中选择了两个文本,包括它们之间的自由文本,正如您在回复中所描述的。我会试着用你的正则表达式替换他的正则表达式,看看它是否有效。@Richard77:这是一个常见的问题,请参阅“小心贪婪”一节!在本教程中,关于。另外,不要忘记转义引号,如Regex Regex=new Regex\[^\]*\;。