Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.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/5/ruby-on-rails-4/2.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_Match - Fatal编程技术网

C# 从文本中获取三重管道之间的所有字符串,例如| | | | | | |

C# 从文本中获取三重管道之间的所有字符串,例如| | | | | | |,c#,regex,match,C#,Regex,Match,我想从文本中获取所有被三个管道包围的字符串(如| | | | | | | | | |),并在C中找到这个正则表达式: 它在处理小字符串时可以正常工作,但不能处理大文本(冻结而无响应) 您能告诉我如何加快此查询或建议其他方法吗?模式开始时的*?会降低匹配速度,因为一旦后续子模式失败,引擎需要执行更多检查。一旦没有|,*?将被“展开”或“回溯”,非|字符将与*?匹配。对于非常长的字符串,这将导致灾难性的回溯 第二种模式还允许内部优化,因为正则表达式引擎知道匹配将以一个硬编码字符开始 您需要删除*?,

我想从文本中获取所有被三个管道包围的字符串(如
| | | | | | | | | |
),并在C中找到这个正则表达式:

它在处理小字符串时可以正常工作,但不能处理大文本(冻结而无响应)


您能告诉我如何加快此查询或建议其他方法吗?

模式开始时的
*?
会降低匹配速度,因为一旦后续子模式失败,引擎需要执行更多检查。一旦没有
|
*?
将被“展开”或“回溯”,非
|
字符将与
*?
匹配。对于非常长的字符串,这将导致灾难性的回溯

第二种模式还允许内部优化,因为正则表达式引擎知道匹配将以一个硬编码字符开始

您需要删除
*?
,因为您不需要在
||| | | | |
之前的零件

您可以比较和匹配以下步骤:

第一个:

第二个:


您可以在第一张图像中看到表示回溯的“红色箭头”更频繁地发射。

为什么在开始时使用
*?
?只使用
\\\\\\\\\\(\w+)\\\\\\\\\\\\\\\\\
。因为我对正则表达式缺乏了解。它解决了它,非常感谢!
Regex regex = new Regex(@".*?\|\|\|(\w+)\|\|\|"); // searches strings, which are surrounded by three pipes >>> |||string|||
foreach (Match match in regex.Matches(strContent))
{
    lstReturn.Add(match.Groups[1].Value);
}