Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.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,我想捕获一个正则表达式结果,以便在整个代码中使用它。例如,我使用正则表达式去除字符串中的特定部分,并希望在字符串变量中捕获结果 甚至有可能做这样的事吗?你是怎么做到的 输入: C:\Users\Documents\Development\testing\11.0.25.10\W\u 11052\u 0\u X.pts 希望存储到字符串中的预期结果: C:\Users\Documents\Development\testing\11.0.25.10\ 正则表达式模式: ^(.*[\])当然,您可以

我想捕获一个正则表达式结果,以便在整个代码中使用它。例如,我使用正则表达式去除字符串中的特定部分,并希望在字符串变量中捕获结果

甚至有可能做这样的事吗?你是怎么做到的

输入:

C:\Users\Documents\Development\testing\11.0.25.10\W\u 11052\u 0\u X.pts

希望存储到字符串中的预期结果:

C:\Users\Documents\Development\testing\11.0.25.10\

正则表达式模式:
^(.*[\])

当然,您可以:
系统的
属性。Text.RegularExpressions.Match
对象允许您通过访问相应组的
属性,以
字符串
的形式访问匹配项

例如,您可以这样做来捕获示例中预期输出的值:

string nameString = @"C:\Users\Documents\Development\testing\11.0.25.10\W_11052_0_X.pts";
// Note that I needed to double the slashes in your pattern to avoid the "unmatched [" error
string pathPrefix = Regex.Match(nameString, @"^(.*[\\])").Groups[1].Value;
Console.WriteLine(pathPrefix);
上面的照片

C:\Users\Documents\Development\testing\11.0.25.10\

这是一个。

请提供示例和您尝试过的内容。那么您想存储正则表达式捕获并在其他地方使用它进行比较吗?@Zekth我不知道该怎么做,所以我没有尝试过任何东西。@bradcristie我想是这样的?请提供带有输入和预期结果的示例。您应该在后面附加
Groups[1]
或将
numbericId
的类型更改为
Group
@KingKing谢谢!我太草率地从记忆中把它打出来了。我试过之后,这个错误很快就暴露出来了:-)谢谢!这正是我需要的。