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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

.NET regex

Status
Not open for further replies.

gorgor

Programmer
Aug 15, 2002
164
US
I always think I understand regex's until I go to use them. I'm using C# and .NET Framework. Let's say I have the following string:

"C:\\MsDev\\Sharp\\bin\\Debug\\Control.dll`~`3`~`0`~`" notice that `~` is my delimiter

I want to extract the filename from the string using regex.

I think the following snippet should work, but it is assigning 'filename' with the entire string, not just the filename. Any ideas?? Thanks in advance.

Code:
//Get the filename from string using regex...
string filename = (Regex.Replace(dataString, "^(.*)(`~`)?", "$1")).ToString();
 
The whole string matches always with (.*).
Yopu should change the pattern or use the following statements to extract the file name:
Code:
string sPath = "C:\\MsDev\\Sharp\\bin\\Debug\\Control.dll`~`3`~`0`~`" ;  
string sFileName = (sPath.IndexOf("`~`") == -1 )? sPath : sPath.Substring(0,sPath.IndexOf("`~`"));
-obislavu-
 
Any reason why you can't use the Split method on the String object?
Code:
string delimStr = "~";
char [] delimiter = delimStr.ToCharArray();
string [] split = sPath.Split(delimiter);
sFileName = split[0];
Chip H.



If you want to get the best response to a question, please check out FAQ222-2244 first
 
I'll be using the split later. Before I do the split, I need to extract the filename (and a couple other things)from the string. Obislavu's suggestion works perfectly. Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top