Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

Remove leading path and extension from filename

Last post 08-20-2012, 3:56 AM by g1smd. 8 replies.
Sort Posts: Previous Next
  •  02-08-2011, 6:12 AM 77796

    Remove leading path and extension from filename

    How can I extract a filename from a path construct, such as:

     /a/very/long path/filename.ext1.ext => filename

     

    Filed under:
  •  02-08-2011, 10:20 AM 77815 in reply to 77796

    Re: Remove leading path and extension from filename

    Please read the Posting Guidelines and provide as much of the requested information as possible. Thanks.

    Michael

    "In theory, theory and practice are the same. In practice, they are not."
    Albert Einstein
  •  02-08-2011, 1:14 PM 77827 in reply to 77796

    Re: Remove leading path and extension from filename

    You could try the regex: (\w+)(?:\.\w+)*$

    Capture group 1 will contain the filename. 

  •  02-08-2011, 5:25 PM 77834 in reply to 77796

    Re: Remove leading path and extension from filename

    Most of the operating systems I know of provide (file system) API calls that will do exactly what you want.

    Susan

  •  02-09-2011, 3:37 PM 77885 in reply to 77834

    Re: Remove leading path and extension from filename

    .*?([^/\.]+)\..*

     for "/"-separated paths

     

  •  08-17-2012, 3:24 AM 86187 in reply to 77885

    Re: Remove leading path and extension from filename

    ^(([^/]+/)*)(([^/.]+)\..+)

    Filename without extension will be in $4.

    Filename with extension will be in $3.

     

     

     

     

    Filed under:
  •  08-17-2012, 4:49 PM 86200 in reply to 77815

    Re: Remove leading path and extension from filename

    mash:
    Please read the Posting Guidelines and provide as much of the requested information as possible. Thanks.

     

    Hi, Michael,

    can you pls post those guidelines, I cannot see them ;-)

    and what's up with this sudden avalanche of posts today? smth moved deep down?

    Sergei Z 

     

  •  08-19-2012, 7:14 PM 86220 in reply to 86200

    Re: Remove leading path and extension from filename

    Hello Sergei,

    Michael announced a few months ago that he would not be following this forum any more - for some reason the number of questions dropped dramatically and he was moving on to other areas in his life (I think I have summarised it correctly -apologies if not).

    If you have a look at the increase in posts, they are all old posts (some 3 or more years old) that have been resurrected. Many seen to be about parsing phone numbers. I would doubt if the original posters of those questions are still looking for further answers but thety might assist others who find themselves in the same situation and are looking for existing answers.

    Susan

  •  08-20-2012, 3:56 AM 86229 in reply to 86220

    Re: Remove leading path and extension from filename

    I'm working on a couple of those projects and found a lot of conflicting and/or out of date information via Google.

    Having finally worked out what the correct information should be, it made sense to post it in a few places where it is likely to be seen by others asking the same questions that I am...

     

    Most of my RegEx stuff is usually to do with htaccess and mod_rewrite, and I'll hopefully post a few tips and answers about that over the next few months.

     

    I had to contact one of the site owners or moderators to get my forum account activated. Until that time, all I had was a message something like 'This account needs to be approved before you can use it. We will send you an email once the account has been approved'. That took almost a week.

     

View as RSS news feed in XML