C# 帮助我使用RegExp拆分字符串

C# 帮助我使用RegExp拆分字符串,c#,regex,C#,Regex,请帮我解决这个问题。 我想将“-action=1”拆分为“action”和“1” 我不知道为什么结果的长度为4 result[0] = "" result[1] = "action" result[2] = "1" result[3] = "" 请帮帮我 我正在使用.NET2.0 谢谢 您好,我测试了字符串:@“-destination=C:\Program Files\Release”,但结果不准确,我不明白为什么结果的长度为1。我想是因为它的字符串中有一个空格 我想将其拆分为“目标”和“C

请帮我解决这个问题。 我想将“-action=1”拆分为“action”和“1”

我不知道为什么结果的长度为4

result[0] = ""
result[1] = "action"
result[2] = "1"
result[3] = ""
请帮帮我

我正在使用.NET2.0

谢谢


您好,我测试了字符串:@“-destination=C:\Program Files\Release”,但结果不准确,我不明白为什么结果的长度为1。我想是因为它的字符串中有一个空格

我想将其拆分为“目标”和“C:\Program Files\Release”

更多信息:这是我的要求: -string1=string2->将其拆分为:string1和string2。 在string1和string2中,不包含字符:“-”、“=”,但它们可以包含空格


请帮帮我。谢谢。

使用string.split()有什么问题吗

我承认,虽然这仍然提供了可能比您希望在拆分数组中看到的更多的参数

[0] = ""
[1] = "action"
[2] = "1"

不要使用拆分,只需使用
匹配
,然后按索引(索引1和索引2)从
集合中获取结果

尝试此操作(更新为添加
Regex.Split
):

编辑:使用您的示例:

string input = @"-destination=C:\Program Files\Release -action=value";
foreach(Match match in Regex.Matches(input, @"-(?<name>\w+)=(?<value>[^=-]*)"))
{
    Console.WriteLine("{0}", match.Value);
    Console.WriteLine("\tname  = {0}", match.Groups["name" ].Value);
    Console.WriteLine("\tvalue = {0}", match.Groups["value"].Value);
}
Console.ReadLine();
string input=@“-destination=C:\Program Files\Release-action=value”;
foreach(Regex.Matches中的匹配(输入@“-(?\w+)=(?[^=-]*)”)
{
WriteLine(“{0}”,match.Value);
Console.WriteLine(“\tname={0}”,match.Groups[“name”].Value);
Console.WriteLine(“\tvalue={0}”,match.Groups[“value”].value);
}
Console.ReadLine();

当然,如果您的路径在.NET正则表达式中包含
-
字符,则此代码会出现问题,您可以命名您的组

string pattern = @"^-(?<MyKey>\S+)=(?<MyValue>\S+)$";
Regex regex = new Regex(pattern);
string myText = "-action=1";
在他的演讲中,Mark Dominus介绍了学习Perl作者(和)Randal Schwartz的以下有用规则:

  • 当您知道要保留的内容时,请使用捕获或
    m//g
    [或
    regex.Match(…)
    ]
  • 当您知道要扔掉什么时,请使用
    split

发生了什么?您不会免费获得输入验证,它可能会以不同的方式进行处理(想象一下
-range=1-2
)。当然,但是使用RegEx,您不会免费获得任何东西,正如OP所看到的,构建适当的RegEx字符串并不是一项简单的任务。我只是建议一个合理可行的替代方案,它不依赖于知道未知的内容(参见OP的编辑),您永远不应该在.NET正则表达式中使用按索引分组。。。您可以根据原因命名这些组。想象一下,如果有人稍后通过添加另一组括号稍微更改了正则表达式。那你就必须改变你所有的索引我只在非常简单的情况下使用索引,比如这个(只有一两个捕获,没有嵌套的组,等等)。否则,我同意使用名称可以使正则表达式更加健壮和易于理解。您好,我使用字符串@“-destination=C:\Program Files\Release进行了测试,但结果不准确。我想是因为它的字符串中有一个空格。我想将其拆分为“destination”和“C:\Program Files\Release”您好,我使用字符串@“-destination=C:\Program Files\Release”进行了测试,但结果不准确,我不明白为什么结果的长度为1。我想是因为它的字符串中有一个空格。我想将其拆分为“目标”和“C:\Program Files\Release”,请帮助我。谢谢
Match match = regex.Match(myText);
if (!match.Success) {
    // the regex didn't match - you can do error handling here
}
string action = match.Groups[1].Value;
string number = match.Groups[2].Value;
string victim = "-action=1";
string[] stringSplit = victim.Split("-=".ToCharArray());
string[] regexSplit = Regex.Split(victim, "[-=]");
string input = @"-destination=C:\Program Files\Release -action=value";
foreach(Match match in Regex.Matches(input, @"-(?<name>\w+)=(?<value>[^=-]*)"))
{
    Console.WriteLine("{0}", match.Value);
    Console.WriteLine("\tname  = {0}", match.Groups["name" ].Value);
    Console.WriteLine("\tvalue = {0}", match.Groups["value"].Value);
}
Console.ReadLine();
string pattern = @"^-(?<MyKey>\S+)=(?<MyValue>\S+)$";
Regex regex = new Regex(pattern);
string myText = "-action=1";
Match theMatch = regex.Match(myText);
if (theMatch.Success)
{
    Console.Write(theMatch.Groups["MyKey"].Value); // This is "action"
    Console.Write(theMatch.Groups["MyValue"].Value); // This is "1"
}