Regex errors parsing file path

Hi,

I'm trying to split a string that holds the full path to a directory using Regex(i.e Regex *regex = new Regex(S"\\");, however I keep getting the following error :

parsing "\" - Illegal at end of pattern

Parameter name: \.

Is there any other way to split up the directories, or to fix this error?

(Moderator: Thread moved to the Regular Expression Forum and Title tweaked for quicker thread understanding during a search)
[521 byte] By [ChrisAus] at [2008-2-13]
# 1
Never mind, if fixed it by replacing the backslash with a forward slash and then doing the split.
ChrisAus at 2007-8-30 > top of Msdn Tech,.NET Development,Regular Expressions...
# 2
Hi Chris,
Add '@' to the "\\". My example code is:


Regex rx = new Regex(@"\\$");

string[] tests = { @"\first-last\", @"last\", @"\first", @"no"};

foreach (string test in tests)
{
if (rx.IsMatch(test))
{
MessageBox.Show(string.Format("{0} Matches.", test));
}
else
{
MessageBox.Show(string.Format("{0} Doesn't match", test));
}
}


It works fine.
Besides, you can use System.IO.Path to manipulate your path.
Hope this helps.
gqlu at 2007-8-30 > top of Msdn Tech,.NET Development,Regular Expressions...

.NET Development

Site Classified