Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/320.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,我有下面的字符串,我想用开始字符和结束字符分割字符串,开始字符是format,结束字符是end,所以 format_abc_endformat_def_endformat_ghi_end 结果需要是format_abc_endformat_def_endformat_ghi_end我正在尝试下面的正则表达式:(?:^ s)format_(.*)end(?:\s |$),它似乎在中起作用,但在它中使用时并不像我想象的那样起作用 使用此正则表达式: format_(.*?)_end 第二个案例不

我有下面的字符串,我想用开始字符和结束字符分割字符串,开始字符是format,结束字符是end,所以

format_abc_endformat_def_endformat_ghi_end
结果需要是
format_abc_end
format_def_end
format_ghi_end
我正在尝试下面的正则表达式:
(?:^ s)format_(.*)end(?:\s |$)
,它似乎在中起作用,但在它中使用时并不像我想象的那样起作用

使用此正则表达式:

format_(.*?)_end
第二个案例不起作用的原因是: 您的第二个案例regex:

(?:^|\s)format_(.*?)_end(?:\s|$)
字符串:格式\u abc\u endformat\u def\u endformat\u ghi\u end

^
=查找字符串的开头。这是一个连续字符串,因此如果添加此字符串,它将只匹配一次。

\s
=查找空白字符。字符串中的空白在哪里

您可以使用此解决方案:):


格式。*?结束
。模式要求是什么
(?:^ |\s)
匹配字符串或空格的开头<代码>(?:\s |$)匹配空格或字符串结尾。第二个测试用例很好地匹配了模式,字符串中没有空格,因此
^
$
在两端都匹配。我所说的方法是什么?正确的方法是什么?就像我说的,结果必须是两种形式的,一种是abc,一种是def,一种是ghi,你试过了吗?我在这里使用了一个命名组,但实际上你并不真正需要它。
using System;
using System.IO;
using System.Text;

class Test
{
    public static void Main()
    {
        string strToProcess = "format_abc_endformat_def_endformat_ghi_end";
        char[] splitchar = { '@' };
       String[] Result =  strToProcess.Replace("_end", "@").Replace("format_", "@").Replace("@@", "@").Split(splitchar);
    }
}