0

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
  • I have an idea but I have been working flat out for 3 hrs now so ... = ) Commented Aug 29, 2009 at 14:58

3 Answers 3

3

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

2 Comments

This ok but am not working with paths, yes is something else all together. Thanks anyways.
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.
1

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.

1 Comment

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.
0
Regex r = new Regex("([A-Z]:\\)([A-Za-z]+)");
Match m = r.Match(@"C:\");

string val1 = m.Groups[0];
string val2 = m.Groups[1];

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.