Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

reg expression: want stuff after "\" in a string 2

Status
Not open for further replies.

G00GLER

Instructor
May 17, 2005
57
US
string in question happens to be "CM\WilliS"
This Code Works:
Code:
					string t = User.Identity.Name;
					string s = Regex.Replace(t, "CM","");
					Message.InnerHtml += s+"<BR>";

But returns "\Willis"
I am having dificulty removing the \


 
Regex.Replace(s, @"[^\w\.@-]", "");

removed the special characters, i guess i needed the brackets
 
a solution specific to my question:
t = Regex.Replace(s, "\\\\", "");
//yes 4 slashes equals one
 
Or an alternative without using a regex:
Code:
        Dim s As String = "CM\WilliS"
        s = s.Substring(s.IndexOf("\") + 1)


--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
in c sharp why won't this work

TextBox1.Text = strFileName2.Substring(strFileName2.IndexOf("\\\\"),+1);

TextBox1.Text is returning "C:\Documents and Settings\delora\Desktop\12.htm"

when I want it to return 12.htm
 
shooting from the hip
System.IO.Path.GetFileName
Marty
 
Not quite

string strFileName2 = FILE2.PostedFile.FileName;
FILE2.PostedFile.FileName is the file returned when browsed on clients computer

which returns as an example "C:\Documents and Settings\delora\Desktop\12.htm":
 
string sFileName = System.IO.Path.GetFileName("C:\Documents and Settings\delora\Desktop\12.htm")

sFileName will have 12.htm

Marty
 
You try your post I gave you a star but I can't get it to work I even spelled it out

TextBox1.Text = System.IO.Path.GetFileName("C:\Documents and Settings\delora\Desktop\xi.htm");

it won't compile: Unrecognized escape sequence
 
Try doubling up the slashes e.g. \\


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
i need to take in a dynamic file path and out put the file name

so are you suggesting i do a search in replace and add \\ where there are only one then use System.IO.Path.GetFileName();
 
Thanks you both really appreciate your suggestions,

I ended up getting it to work using the split function and choosing the last in an array.
 
GOOGlER,
Just saw this looking for something. Glad you solved your problem. For future reference if you get Unrecognized escape sequence
it can usually be fixed by usig ca8msm's \\
TextBox1.Text = System.IO.Path.GetFileName("C:\\Documents and Settings\\delora\\Desktop\\xi.htm");
or
TextBox1.Text = System.IO.Path.GetFileName(@"C:\Documents and Settings\delora\Desktop\xi.htm");

did'nt know which language you where using using.
Marty
 
But surely this is only an artefact of putting the file name in as a literal in the test above. If you already have the file name in a string variable, then you shouldn't need to worry about escaping the backslashes.
Code:
TextBox1.Text = System.IO.Path.GetFileName(strFileName2);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top