Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/256.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/20.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# 在reg ex中查找特定单词和特殊字符 string emailBody=“holla holla测试用于新财务历史记录:\'xyz\'dsd新财务历史记录:\'abc\'NewEBTDI$:\'abc\'dsd”; emailBody=string.Join(“,Regex.Split(emailBody.Trim(),@”(?:\r\n| \n| \r)”); var keys=Regex.Matches(emailBody,@“\bNew\B(+?):”,RegexOptions.Singleline)。of type()。选择(m=>m.Groups[0]。Value.Replace(“:”,”“)。Distinct()。ToArray(); foreach(字符串键入键) { 列表值列表=新列表(); 字符串regex=”“+key+”:“+”\”(?[^\“]*)\”; var matches=Regex.matches(emailBody、Regex、RegexOptions.Singleline); foreach(匹配中的匹配) { 如果(匹配成功) { 字符串值=match.Groups[GetCleanKey(key)].value; 如果(!valueList.Contains(value.Trim())) { valueList.Add(value.Trim()); } } } 公共字符串GetCleanKey(字符串键) { 返回键。替换(“,”)。替换(“-”,”)。替换(“#“,”)。替换(“$”,”)。替换(“*”,”)。替换(“!”,”)。替换(“@”,”) .Replace(“%,”).Replace(“^”和“).Replace(“&”和“).Replace(“(”,”).Replace(“[”,”).Replace(“],”).Replace(“?”,”) .Replace(“,”).Replace(“,”).Replace(“;”,”).Replace(“/”,”).Replace(“\”,”).Replace(“+”,”).Replace(“~”,”).Replace(“,”) .Replace(“{,”).Replace(“},”).Replace(“+”,”).Replace(“|“,”); }_C#_Regex_Regex Lookarounds_Regex Greedy_Boost Regex - Fatal编程技术网

C# 在reg ex中查找特定单词和特殊字符 string emailBody=“holla holla测试用于新财务历史记录:\'xyz\'dsd新财务历史记录:\'abc\'NewEBTDI$:\'abc\'dsd”; emailBody=string.Join(“,Regex.Split(emailBody.Trim(),@”(?:\r\n| \n| \r)”); var keys=Regex.Matches(emailBody,@“\bNew\B(+?):”,RegexOptions.Singleline)。of type()。选择(m=>m.Groups[0]。Value.Replace(“:”,”“)。Distinct()。ToArray(); foreach(字符串键入键) { 列表值列表=新列表(); 字符串regex=”“+key+”:“+”\”(?[^\“]*)\”; var matches=Regex.matches(emailBody、Regex、RegexOptions.Singleline); foreach(匹配中的匹配) { 如果(匹配成功) { 字符串值=match.Groups[GetCleanKey(key)].value; 如果(!valueList.Contains(value.Trim())) { valueList.Add(value.Trim()); } } } 公共字符串GetCleanKey(字符串键) { 返回键。替换(“,”)。替换(“-”,”)。替换(“#“,”)。替换(“$”,”)。替换(“*”,”)。替换(“!”,”)。替换(“@”,”) .Replace(“%,”).Replace(“^”和“).Replace(“&”和“).Replace(“(”,”).Replace(“[”,”).Replace(“],”).Replace(“?”,”) .Replace(“,”).Replace(“,”).Replace(“;”,”).Replace(“/”,”).Replace(“\”,”).Replace(“+”,”).Replace(“~”,”).Replace(“,”) .Replace(“{,”).Replace(“},”).Replace(“+”,”).Replace(“|“,”); }

C# 在reg ex中查找特定单词和特殊字符 string emailBody=“holla holla测试用于新财务历史记录:\'xyz\'dsd新财务历史记录:\'abc\'NewEBTDI$:\'abc\'dsd”; emailBody=string.Join(“,Regex.Split(emailBody.Trim(),@”(?:\r\n| \n| \r)”); var keys=Regex.Matches(emailBody,@“\bNew\B(+?):”,RegexOptions.Singleline)。of type()。选择(m=>m.Groups[0]。Value.Replace(“:”,”“)。Distinct()。ToArray(); foreach(字符串键入键) { 列表值列表=新列表(); 字符串regex=”“+key+”:“+”\”(?[^\“]*)\”; var matches=Regex.matches(emailBody、Regex、RegexOptions.Singleline); foreach(匹配中的匹配) { 如果(匹配成功) { 字符串值=match.Groups[GetCleanKey(key)].value; 如果(!valueList.Contains(value.Trim())) { valueList.Add(value.Trim()); } } } 公共字符串GetCleanKey(字符串键) { 返回键。替换(“,”)。替换(“-”,”)。替换(“#“,”)。替换(“$”,”)。替换(“*”,”)。替换(“!”,”)。替换(“@”,”) .Replace(“%,”).Replace(“^”和“).Replace(“&”和“).Replace(“(”,”).Replace(“[”,”).Replace(“],”).Replace(“?”,”) .Replace(“,”).Replace(“,”).Replace(“;”,”).Replace(“/”,”).Replace(“\”,”).Replace(“+”,”).Replace(“~”,”).Replace(“,”) .Replace(“{,”).Replace(“},”).Replace(“+”,”).Replace(“|“,”); },c#,regex,regex-lookarounds,regex-greedy,boost-regex,C#,Regex,Regex Lookarounds,Regex Greedy,Boost Regex,在我上面的代码中,我试图获取NewEBTDI$:旁边的值,即“abc” 当我在模式中包含$符号时,它不会搜索字段名旁边的值 如果删除了$,并且只指定了NewEBTDI,那么它将搜索这些值 我想搜索值和$符号。处理在正则表达式中具有特殊含义但必须按原样搜索的字符的正确方法是转义。您可以使用regex.escape来执行此操作。在您的情况下,它是$符号,如果不转义,则表示正则表达式中的行尾 string emailBody = " holla holla testing is for NewFina

在我上面的代码中,我试图获取
NewEBTDI$:
旁边的值,即
“abc”

当我在模式中包含
$
符号时,它不会搜索字段名旁边的值

如果删除了
$
,并且只指定了
NewEBTDI
,那么它将搜索这些值


我想搜索值和
$
符号。

处理在正则表达式中具有特殊含义但必须按原样搜索的字符的正确方法是转义。您可以使用
regex.escape
来执行此操作。在您的情况下,它是
$
符号,如果不转义,则表示正则表达式中的行尾

string emailBody = " holla holla testing is for NewFinancial History:\"xyz\"  dsd  NewFinancial History:\"abc\"  NewEBTDI$:\"abc\"  dsds  ";

   emailBody = string.Join(" ", Regex.Split(emailBody.Trim(), @"(?:\r\n|\n|\r)"));
                var keys = Regex.Matches(emailBody, @"\bNew\B(.+?):", RegexOptions.Singleline).OfType<Match>().Select(m => m.Groups[0].Value.Replace(":", "")).Distinct().ToArray();
                foreach (string key in keys)
                {
                    List<string> valueList = new List<string>();
                    string regex = "" + key + ":" + "\"(?<" + GetCleanKey(key) + ">[^\"]*)\"";

                    var matches = Regex.Matches(emailBody, regex, RegexOptions.Singleline);
                    foreach (Match match in matches)
                    {
                        if (match.Success)
                        {
                            string value = match.Groups[GetCleanKey(key)].Value;
                            if (!valueList.Contains(value.Trim()))
                            {
                                valueList.Add(value.Trim());
                            }
                        }
                    }

 public string GetCleanKey(string key)
        {
            return key.Replace(" ", "").Replace("-", "").Replace("#", "").Replace("$", "").Replace("*", "").Replace("!", "").Replace("@", "")
                .Replace("%", "").Replace("^", "").Replace("&", "").Replace("(", "").Replace(")", "").Replace("[", "").Replace("]", "").Replace("?", "")
                .Replace("<", "").Replace(">", "").Replace("'", "").Replace(";", "").Replace("/", "").Replace("\"", "").Replace("+", "").Replace("~", "").Replace("`", "")
                .Replace("{", "").Replace("}", "").Replace("+", "").Replace("|", "");
        }
string regex=“”+regex.Escape(key)+“:“+”\”(?[^\“]*)\”;

string regex=string.Format(“{0}:\”(?[^\”]*)\”,
Regex.Escape(键),
Regex.Escape(GetCleanKey(key));
或使用VS 2015,使用字符串插值:

string regex = String.Format("{0}:\"(?<{1}>[^\"]*)\"",
                             Regex.Escape(key),
                             Regex.Escape(GetCleanKey(key)));
string regex=$“{regex.Escape(key)}:\”(?[^\“]*)\”;

(它看起来确实比实际情况要好,因为C#编辑器对字符串部分和嵌入的C#表达式的颜色不同。)不清楚最终目标是什么,但是模式中的
$
是一种模式转义,它意味着行的结尾,或者缓冲区的结尾,这取决于是否设置了
多行

为什么不将
前面的文本捕获到命名的捕获中?然后提取引用的操作值,例如:

string regex = $"{Regex.Escape(key)}:\"(?<{Regex.Escape(GetCleanKey(key))}>[^\"]*)\"";
var data=“…用于新财务历史记录:\“xyz\”dsd新财务历史记录:\“abc\”新EBTDI$:\“abc\”dsd;
变量模式=@“
(?New[^:]+)#捕获“New”之后的所有项目,即*不是*(`^`)a`:`,一个或多个。
:#实际`:`
\x22#实际引号字符开始锚定
(?[^\x22]+)#不是引号的文本,一个或多个
\x22#实际报价结束锚定
";
//IgnorePatternWhitespace允许我们对模式进行注释。不影响处理。
Regex.Matches(数据、模式、RegexOptions.IgnorePatternWhitespace | RegexOptions.ExplicitCapture)
第()类
.选择(mt=>new
{
NewText=mt.Groups[“New”]值,
Text=mt.Groups[“InQuotes”].值
});
结果


注意:我使用十六进制转义
\x22
,而不是在模式中转义
\“
,以便于使用。因为它避免了C编译器过早转义需要保持完整的模式转义。

请正确安排代码。它不可读。“$”在正则表达式中有一个特殊的含义。用\来转义它。但是在您的情况下,您必须执行String.Replace()方法,因为您的正则表达式是生成的。您可能还有其他特殊字符……我不知道正则表达式。转义!
string regex = $"{Regex.Escape(key)}:\"(?<{Regex.Escape(GetCleanKey(key))}>[^\"]*)\"";
var data = "...is for NewFinancial History:\"xyz\"  dsd  NewFinancial History:\"abc\"  NewEBTDI$:\"abc\"  dsds";

var pattern = @"
(?<New>New[^:]+)      # Capture all items after `New` that is *not* (`^`) a `:`, one or more.
:                     # actual `:`
\x22                  # actual quote character begin anchor
(?<InQuotes>[^\x22]+) # text that is not a quote, one or more
\x22                  # actual quote ending anchor
";

// IgnorePatternWhitespace allows us to comment the pattern. Does not affect processing.
Regex.Matches(data, pattern, RegexOptions.IgnorePatternWhitespace | RegexOptions.ExplicitCapture)
     .OfType<Match>()
     .Select(mt => new
     {
         NewText = mt.Groups["New"].Value,
         Text = mt.Groups["InQuotes"].Value
     });