How do make a regular expression to match file paths?

regular expression for directory path checking
file path - regex
how to create regular expression
regex for file path python
file path validation in java
regular expression for file path checking
regex tester
regular expression for directory path checking linux

I've been playing with this command for about an hour or two and I'm afraid I may have lost objectivity. The goal is to match only relative file paths given to bash.

The first relative path . or ./some/file/path

The second relative path is .. or ../some/file/path

Where the length of "/some/file/path" is arbitrary.

I've been using grep within bash to try and figure out how to implement it in to my script so that I can expand it to it's absolute file path so that ./some/file/path or ../some/file/path becomes /the/absolute/file/path; which I've already figure out.

My problem is matching the relative path.

The code I've been using is

echo "../some/file/path" | egrep '\.{1}/?[[:graph:]]?+$'


echo "../some/file/path" | egrep '\.{2}/?[[:graph:]]?+$'

and I've narrowed my issue down to being

echo ".." | egrep '\.{2}'

will match the dot as long as it has 2 + n occurrences, not exactly 2 occurrences as expected. The same thing happens when I change it to

echo ".." | egrep '\.{1}' 

will still match for some reason I can't figure out.

The final implementation is supposed to work something like this

 41 _expand_relative_path () {
 42         if [[ "$1" =~ ^\.{1}/?[[:graph:]]?+$ ]]; then
 43                 echo "."
 44         elif [[ "$1" =~ ^\.{2}/?[[:graph:]]?+$ ]]; then
 45                 echo ".."
 46         else    
 47                 echo "$1"
 48         fi
 49 }

According to my text book, the specifier {n} will match the preceding element if it occurs exactly n times. But it doesn't do that! It matches if it is n or more times! What am I doing wrong?

The regex that matches a relative path is one that doesn't start with a slash:


Advanced Search, This RegEx will help to validate a physical file path with a specific file extension (​here xls). Matches. E:\DyAGT\SD01A_specV2.xls. Non-Matches. Regular Expression to Matches file and folder path structure in Windows - all versions.

the issue with ^\.{1}/?[[:graph:]]?+$ is that the / has been designated as optional, and the following [[:graph:]] character class matches anything visible, including more periods. also, you've quantified your character class with ?+, which means "zero or once, possessive": it ain't gotta match, but if it does, will not "give up" what it matched to let the rest of the pattern try to succeed - probably not what you wanted there.

when you say echo ".." | egrep '\.{2}', what you're saying is "string contains, at some point, two periods in a row" - but that doesn't mean it can't have more periods or anything else, not without ^ and $ anchors anyway, which would limit to exactly and only two periods.

as others note, any path not starting with / is relative so ^[^/].* works. But if you wanted to find relative paths that are in a text file with some other text, this may be useful:


regex demo output:

How do make a regular expression to match file paths?, The regex that matches a relative path is one that doesn't start with a slash: ^[^/].*. I have want to match files located in multiple directories: The file path could be locally - C:/users/path/image.png or on a system - / /home/user/web/image.png For the first case, I have regular expression -

Regular Expression to Validate File Path and Extension, Regular Expression to validate the file path and extension and it is I hope someone will find this information useful and that it will make your Non-​Matches. Regular Expression to validate the file path and extension and it is compatible with JavaScript and ASP.NET

For Windows: ^.*\\(?!.*\\)(.*)$

or for Linux: ^.*/(?!.*/)(.*)$

or for both:


It matches the filename.extension in a .../path/filename.extension or ...\path\filename.extension as it checks for the last occurrence of either \ or / and then captures every character from that point forward.

Regex to match file paths at root - Server Config, Following regex matches the ensuing URL except the first one, and I can't seem to get it right with roping such links in, as well as retaining the  Regex that matches path, filename and extension April 6, 2009 4 Comments I was looking for a regular expression for Python capable to match a string containing a valid path, file name and extension.

Pattern matching guide, Fisheye supports a powerful type of regular expression for matching files and directories (same as the pattern matching in Apache Ant). If the pattern does not start with a path separator i.e. / or \, then the pattern is  You cannot do this with only list.files because it loops over each element in path and applies the regular expression to the files contained therein. But since the path argument to list.files can accept a vector, you can use that to solve your problem.

Windows OS file and folder path, RegEx for Matches file and folder path structure in Windows - all versions. Regular Expression Basics. In a nutshell, regular expressions represent a rule set for performing pattern matching on string data. You're probably familiar with using the old MS-DOS wildcard characters. For instance, we can run the following command at the prompt to find all .xls or .xlsx files in the current folder whose names contain the word report:

8.18. Validate Windows Paths, Drive letter paths. Matching a full path to a file Get Regular Expressions Cookbook, 2nd Edition now  I want to search a directory for some files using find and a regular expression. path with find when searching for files with -regex match the the regular

  • A path that does not start with / is relative..
  • you're saying that '^[^/]\.?\.?/?[[:graph:]]+$' is the simpler expression. is there a way to identify the . element exactly n times with the rest of my expression?
  • Why do you need this? And no, a rwkative path is just a [^/].*.
  • I noticed you used a pcre, I haven't seen the : before. What does that do? For example (?:\/[[ within the second subexpression.
  • Those are grouping only parens (?:pattern), they do not capture. You might need pcregrep instead of egrep to use those...
  • I thought the parentheses ( ) were for grouping? I meant the colon : specifically within the subexpression, right after the ? quantifier for matching an element zero or one time.
  • (Pattern) would capture Pattern in the match result and work for grouping, but (?:Pattern) only groups and does not capture anything
  • And actually the outer parentheses aren't necessary here
  • I really like how you broke down my expression and explained what was happening at each step. You are correct in your assumptions. Although, it is my fault, I left out the fact that ./ and ../ were both allowed as well which explains why I made that optional.