3

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.

1
  • 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" Please help me. Thanks. Commented Jan 20, 2010 at 11:39

5 Answers 5

5

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;
Sign up to request clarification or add additional context in comments.

3 Comments

You should never use groups by index in .NET Regex... you can name the groups for a reason. Imagine if someone slightly changed the regex later by adding another set of parenthesis. Then you'd have to change all your indexes everywhere. - Use group names instead.
I only use indexes for very simple cases, such as this one (just one or two captures, no nested groups, etc.). Otherwise, I agree that using names makes the regex much more robust and easier to understand.
Hello, I tested with string: @"-destination=C:\Program Files\Release" but it have inaccurate result. I think because it has a white space in string. I want to split it to "destination" & "C:\Program Files\Release"
3

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

Comments

3

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"
}

Comments

0

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"

2 Comments

What's wrong? You don't get input validation for free, and it may handle things differently (imagine -range=1-2).
Granted, but with RegEx, you don't get anything for free, and as the OP has seen, it is not a trivial task to build the appropriate regex string. I'm just suggesting a reasonably workable alternative that doesn't rely on knowing the unknown (see OP's edit)
0

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 [or regex.Match(...)] when you know what you want to keep.
  • Use split when you know what you want to throw away.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.