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

Simple RegEx Problem

Status
Not open for further replies.

Kliot

Programmer
Jan 10, 2003
622
US
I have what I think is a simple RegEx problem, unfortunately for me there is nothing simple with RegEx.

I have a file path and name, I want to extract the file name less the first word (a code number of varying length) and the extension.

eg:
from "c:\My Documents\Records\2096 My FileName.pdf" I want to extract "My FileName"

Any RegEx guru's out there that can help?

Thanks
Perrin
 
Another option:

Code:
Dim test As String
test = "c:\My Documents\Records\2096 My FileName.pdf"
test = CreateObject("Scripting.FileSystemObject").GetBaseName(test)
MsgBox Mid(test, InStr(1, test, " ") + 1)

Swi
 
Swi,

Yes another option that's over my head and it works great, I'll have to spend some time and see how it works. Thanks

I'm still curious about how to do it with RegEx.

Thanks
Perrin
 
I am not a regex expert either. Hopefully someone else can help you out here.

Swi
 
here is an example using regex:

Dim test As String = "c:\My Documents\Records\2096 My FileName.pdf"
Dim res As String()
Dim fileName As String

res = Regex.Split(test, "\\")
fileName = res(res.Length - 1)
fileName = fileName.Substring(fileName.IndexOf(" ") + 1, fileName.IndexOf(".") - fileName.IndexOf(" ") - 1)

This assumes that there is a space in the filename after the code number
 
or:

Dim test As String = "c:\My Documents\Records\2096 My FileName.pdf"
Dim fileName As String

fileName = Regex.Split(test, "\\").Last.Substring(Regex.Split(test, "\\").Last.IndexOf(" ") + 1, Regex.Split(test, "\\").Last.IndexOf(".") - Regex.Split(test, "\\").Last.IndexOf(" ") - 1)
 
lestatdelioncourt,

Thanks for the code, I was hoping there was a simple regex string that would be able to strip away everyting up to and including the first space after the last "\" and everyting after and including the last "."

Thanks
Perrin
 
I found another pretty simple solution


Dim filePath As String = "c:\my documents\Records\05645 My FileName.pdf"
Dim fileName As String = System.IO.Path.GetFileNameWithoutExtension(filePath)
Dim re As New Regex("^\d*\s")
fileName = re.Replace(fileName, "")
 
If you really want to do this just using a RegEx you can like this:

Code:
Dim filePath As String = "c:\My Documents\Records\2096 My FileName.pdf"

Dim matches As Match = Regex.Match(filePath, "\\(?<code>\d*?)\s(?<fileName>.*?)\.(?<ext>.*?$)")

Dim fileName As String = matches.Groups("fileName").Value
Dim code As String = matches.Groups("code").Value
Dim extention As String = matches.Groups("ext").Value

If all you want is the file name you can miss out the last two lines.
 
Noneed for regex since your problem has been solved for you. If you use the correct library. And the correct one being System.io. Which has a class called Path. Which has a method called getfilenamewithoutextension. Now isn't that a coincidence, that just happens to be what you are looking for.

So here is th function

Code:
dim filename as string = System.IO.Path.GetFileNameWithoutExtension("pathgoeshere")

So no silly regex for this just one line will do.


Christiaan Baes
Belgium

My Blog
"In a system where you can define a factor as part of a third factor, you need another layer to check the main layer in case the second layer is not the base unit." - jrbarnett
 
chrissie1,

System.IO.Path.GetFileNameWithoutExtension does not do what kliot is looking for because this will also return the code number (2096 in the example).
 
Aptitude,

Thanks, that's exactly what I was looking for, I knew there were other solutions but I'm trying to learn more about RegEx.

Chrissie1,

Thanks for the IO tip, I remembered that class this weekend but I still had to strip off the code number.

Thanks to all for the help
Perrin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top