tags:

views:

126

answers:

3

Hi guys,

I need some help extracting the following bits of information using regular expressions.

Here is my input string "C:\Yes"

**** Missing character at start of string and in between but not at the end = a weird superscript looking L.***

I need to extract "C:\" into one string and "Yes" into another.

Thanks In Advance.

+1  A: 

The following regular expression returns C:\ in the first capture group and the rest in the second:

 ^(\w:\\)(.*)$

This is looking for: a full string (^…$) starting with a letter (\w, although [a-z] would probably more accurate for Windows drive letters), followed by :\. All the rest (.*) is captured in the second group.

Notice that this won’t work with UNC paths. If you’re working with paths, your best bet is not to use strings and regular expressions but rather the API found in System.IO. The classes found there already offer the functionality that you want.

Konrad Rudolph
I am goiong to give this a shot, it looks like what I want and seems easy enough to read. I'll hit it up with an answer if it solves my problem. Thanks.
IbrarMumtaz
A: 
Regex r = new Regex("([A-Z]:\\)([A-Za-z]+)");
Match m = r.Match(@"C:\");

string val1 = m.Groups[0];
string val2 = m.Groups[1];
skalburgi
+3  A: 

I wouldn't bother with regular expressions for that. Too much work, and I'd be too likely to screw it up.

var x = @"C:\Yes";

var root = Path.GetPathRoot(x);  // => @"C:\"
var file = Path.GetFileName(x);  // => "Yes"
Alec
This ok but am not working with paths, yes is something else all together. Thanks anyways.
IbrarMumtaz
If it looks like "C:\Yes", even if it's not a path, I'd still use these methods, for exactly the reasons given. Whether it's a config file or not makes no difference to the computer.
Alec