Help me to split string using RegExp
please help me this problem. I want to split "-action=1" to "action" and "1".
string pattern = @"^-(\S+)=(\S+)$";
Regex regex = new Regex(pattern);
string myText = "-action=1";
string[] result = regex.Split(myText);
I don't know why result have length=4.
result[0] = ""
result[1] = "action"
result[2] = "1"
result[3] = ""
Please help me.
P/S: I am using .NET 2.0.
Thanks.
Hello, I tested with string: @"-destination=C:\Program Files\Release" but it have inaccurate result, I don't understand why result's length = 1. I think because it has a white space in string.
I want to split it to "destination" & "C:\Program Files\Release"
More info: This is my requirement: -string1=string2 -> split it to: string1 & string2. In string1 & string2 don't contain characters: '-', '=', but they can contain white space.
Please help me. Thanks.开发者_C百科
Don't use split, just use Match
, and then get the results from the Groups
collection by index (index 1 and 2).
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;
Try this (updated to add Regex.Split
):
string victim = "-action=1";
string[] stringSplit = victim.Split("-=".ToCharArray());
string[] regexSplit = Regex.Split(victim, "[-=]");
EDIT: Using your example:
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();
Of course, this code have issues if your path contains -
character
In .NET Regex, you can name your groups.
string pattern = @"^-(?<MyKey>\S+)=(?<MyValue>\S+)$";
Regex regex = new Regex(pattern);
string myText = "-action=1";
Then do a "Match" and get the values by your group names.
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"
}
What's wrong with using string.split()?
string test = "-action=1";
string[] splitUp = test.Split("-=".ToCharArray());
I admit, though that this still gives you possibly more parameters than you'd like to see in the split up array...
[0] = ""
[1] = "action"
[2] = "1"
In his talk Regular Expression Mastery, Mark Dominus attributes the following helpful rule to Learning Perl author (and fellow StackOverflow user) Randal Schwartz:
- Use capturing or
m//g
[orregex.Match(...)
] when you know what you want to keep.- Use
split
when you know what you want to throw away.
精彩评论