Regular expression problem

S

Steve Barnett

I'm attempting my first regular expression (ok, I nicked some of it from a
blog) and I'm not seeing the results I expect to see. Given that it does not
work, I find myself entirely stuck. Can you help?

I'm trying to split command line parameters specified in the format:
/Startup /Model=a /view="b" /entity="/c"

Where all command line parameters start with a forward slash. They may or
may not have values following an equals sign (I plan to use a variation of
this regex to split key/value pairs) which may also contain a forward slash
(enclosed in quotes), so I need to ignore these in the split.

The code I am using to split the string is:

Regex rex = new
Regex("/(?=([^\\\"]*\\\"[^\\\"]*\\\")*(?![^\\\"]*\\\"))");
string[] values = rex.Split(strCommand);
foreach (string val in values)
System.Windows.Forms.MessageBox.Show(val);

The results I get are strange:
<blank line>
/entity="/c"
startup
/entity="/c"
model=a
/entity="/c"
view="b"
entity="/c"
entity="/c"

I think it has something to do with capturing groups being included in the
string array, but don't know how to detect this and remove them.I ran the
code through a regular expression test app (RegexBudy) and that correctly
identified the delimiters, so I guess the expression itself is correct. I
guess I'm interpreting the output wrongly.

Any suggestions ?

Thanks
Steve
 
S

Steve Barnett

Not sure why it makes a difference, but adding the option
RegexOptions.ExplicitCapture seems to have sorted me out.

Must find myself a tutorial.

Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top