HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptMinor

Regex to select the last components of a file path

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
lastthefilepathselectcomponentsregex

Problem

I have a regex sequence that will take the end of a file path and select it for me:


resources/services/dealerInfoRequest/V5-0/dealerInfoRequestManager.cfc

In this, the regex would select:


/V5-0/dealerInfoRequestManager.cfc

In some cases there is no
V5-0` and it's just the file name and extension. My regex is working as shown but I was wanting to see if there was any optimization I could do, if it could be cleaned up, or if I should just leave it in the order it is now.

Regex is as shown:

((\/)(\w\d-?\d)(\/)\w.+)|(\/)(\w+\.\w+)


It's used in JavaScript and in Sublime Text 3.

Solution

Your regex has too many capturing groups.

Using (?:) makes it into a simple matching group, speeding up the regex since it doesn't have to store anything.

Since you are trying to get the part from the end of the string, anchor it there using $.

At the end, you have this: (\/)(\w+\.\w+).

This is really bad in 2 ways:

  • You are using 2 capturing groups.



  • You regex only allows files with letters. Spaces, underscores and others should de allowed too.



For version control, you have this: (\/)(\w\d-?\d).

This allows me to have a folder called t0-0 which makes no sense.

Be more strict about your match.

This is the regex I came up with:

\/(?:[vV][1-9]\d?(?:-\d)?\/)?[^\/]+\.[^\.]+$


It addresses all your issues.

Explaining each part:

-
(?:\/[vV][1-9]\d?(?:-\d)?\/)? matches the version number, if there is any.

1.1. No need for capturing groups.

1.2. It enforces that the folder starts with v or V with.

1.3. The version only can starts with 1-9 and can have any number or digits.

1.4. The version folder is still optional

-
[^\/]+\.[^\.]+$ matches the file name.

2.1. Again: No need for capturing groups.

2.2. The file can have ANY name.

2.3. The file can have ANY extension, but there must be one.

  • The whole string is matched to the end, which makes it easier for us to write it.

Code Snippets

\/(?:[vV][1-9]\d?(?:-\d)?\/)?[^\/]+\.[^\.]+$

Context

StackExchange Code Review Q#69497, answer score: 3

Revisions (0)

No revisions yet.