I know regular expressions can be used, but am not able to find the right one. Also are there any built-in functions available that do this?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Given:
This regex will pull the pathname:
It looks for a white space character followed by a slash followed by anything. If you don’t have white space in your path names, then you can use the more restrictive:
If you’re sure you’ll always have one or more path components to the names, you can add more restrictions:
And so it goes on. The more you know about what can be valid in the path, the better your chances of matching only the file name. But note that a file name on Unix can contain any character except
/(because it is the delimiter between sections of the path name) and\0, the NUL byte. Everything else – newlines, tabs, controls, etc – is fair game and could be part of a file name. Mercifully, most of them usually aren’t present in file names.Note that relative pathnames are even harder than absolute path names.