Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/313.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,我有一个包含以下内容的字符串:但我不知道如何继续。您的正则表达式将匹配任意数量的字母数字字符,后跟.dtsx。在您的示例中,它将匹配MyPackage10.dtsx @[User::RootPath]+"Dim_MyPackage10.dtsx" 如果要匹配Dim_MyPackage10.dtsx,需要在正则表达式中的允许字符列表中添加下划线:[a-zA-Z0-9]*.dtsx 如果要匹配[User::RootPath],需要一个在最后一个/处停止的正则表达式(或\,取决于在路径中使用的斜杠类型

我有一个包含以下内容的字符串:但我不知道如何继续。

您的正则表达式将匹配任意数量的字母数字字符,后跟
.dtsx
。在您的示例中,它将匹配
MyPackage10.dtsx

@[User::RootPath]+"Dim_MyPackage10.dtsx" 如果要匹配
Dim_MyPackage10.dtsx
,需要在正则表达式中的允许字符列表中添加下划线:
[a-zA-Z0-9]*.dtsx


如果要匹配
[User::RootPath]
,需要一个在最后一个
/
处停止的正则表达式(或
\
,取决于在路径中使用的斜杠类型):类似这样的:
*\/
(或
*\

使用此正则表达式模式:

\[[^[\]]*\]

从答案和评论中检查

,到目前为止,没有一个被“接受”的事实,在我看来,这个问题/问题并不完全清楚。如果您正在寻找模式[User::SomeVariable],其中只有'SomeVariable'是变量,那么您可以尝试:

\[User::\w+]
(?<=\[User::)\w+(?=])
捕捉完整的表情。 此外,如果您希望检测该模式,但只需要“SomeVariable”部分,您可以尝试:

\[User::\w+]
(?<=\[User::)\w+(?=])

(?对于变量,为什么不使用非集合
[^]
提取集合中除外的所有内容

大括号中的
^
表示查找不匹配的内容,例如查找所有非
]
或引号(
)的内容

然后,我们可以将实际匹配项放入命名的捕获组
(?)
,并相应地提取

string pattern = @"(?:@\[)(?<Path>[^\]]+)(?:\]\+\"")(?<File>[^\""]+)(?:"")";
// Pattern is (?:@\[)(?<Path>[^\]]+)(?:\]\+\")(?<File>[^\"]+)(?:")
// w/o the "'s escapes for the C# parser

string text = @"@[User::RootPath]+""Dim_MyPackage10.dtsx""";    

var result = Regex.Match(text, pattern);

Console.WriteLine ("Path: {0}{1}File: {2}",
    result.Groups["Path"].Value,
    Environment.NewLine,
    result.Groups["File"].Value
);

/* Outputs
Path: User::RootPath
File: Dim_MyPackage10.dtsx
*/
string pattern=@”(?:@\[)(?[^\]]+)(?:\]\+\)(?[^\\]]+)(?:);
//模式是(?:@\[)(?[^\]]+)(?:\]\+\”(?:”)((?:”)
//对于C#解析器,没有“”的转义
字符串文本=@“@[User::RootPath]+”Dim_MyPackage10.dtsx”“;
var result=Regex.Match(文本、模式);
Console.WriteLine(“路径:{0}{1}文件:{2}”,
结果.组[“路径”].值,
环境,新线,
result.Groups[“文件”].Value
);
/*输出
路径:用户::根路径
文件:Dim_MyPackage10.dtsx
*/
(?:)
是匹配,但不捕获,因为我们使用它们作为模式的实际锚定,而不是将它们放入匹配捕获组。

这里是bro

using System;
using System.Text.RegularExpressions;
namespace myapp
{
  class Class1
    {
      static void Main(string[] args)
        {
          String sourcestring = "source string to match with pattern";
          Regex re = new Regex(@"\[\S+\]");
          MatchCollection mc = re.Matches(sourcestring);
          int mIdx=0;
          foreach (Match m in mc)
           {
            for (int gIdx = 0; gIdx < m.Groups.Count; gIdx++)
              {
                Console.WriteLine("[{0}][{1}] = {2}", mIdx, re.GetGroupNames()[gIdx], m.Groups[gIdx].Value);
              }
            mIdx++;
          }
        }
    }
}
使用系统;
使用System.Text.RegularExpressions;
名称空间myapp
{
一班
{
静态void Main(字符串[]参数)
{
String sourcestring=“要与模式匹配的源字符串”;
正则表达式re=新正则表达式(@“\[\S+\]”);
MatchCollection mc=re.Matches(sourcestring);
int mIdx=0;
foreach(在mc中匹配m)
{
对于(int gIdx=0;gIdx
谢谢,如果+是@[User::myVariable]+“Dim\u MyPackage10.dtsx”我需要[User::myVariable]欢迎来到StackOverflow并感谢您的发布。请包含一些代码来显示它不应该是
\[^]*\]
?第五个字符是
[
在你的正则表达式中,在我的正则表达式中是
]