1

I need to identify disc files that have three hyphens in the name that may or may not be separated by numbers. The regex needs to be able to cater for this occurring at the end of the file name. I have come up with the following but am sure that there is a more elegant and efficient way to achieve what I want:

string sRegex = @"--[-|.]|-[\d]-[-|.]|--[\d][-|.]|-[\d]-[\d][-|.]";

examples of matching file names are:

Ice Cold Murders- Rocco Schiavone-1-5.ts
Lost Girl---Where There's a Will, There's a Fae_Pick_2019_04_09_20_00.ts
Lost Girl-1-4-Food For Thought_Pick_2019_04_15_20_00.ts

The three hyphens may only be separated by integers or nothing and the numbers represent series number and episode number.

I am writing an app that enters episode numbers starting from a given series and episode number as a series.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
  • 1
    I'm pretty sure you don't understand what square brackets mean in regex. They do not act like brackets; they delimit a character class. For example `[-|.]` matches a single character that is either a `-`, a`|` or a `.`. Also `[\d]` is identical to just `\d`. Read [What is the difference between square brackets and parentheses in a regex?](https://stackoverflow.com/questions/9801630/what-is-the-difference-between-square-brackets-and-parentheses-in-a-regex) – Bohemian May 20 '23 at 11:00
  • 1
    Why does `Ice Cold Murders- Rocco Schiavone-1-5.ts` match? There are only 2 hyphens (separated by digits). – Bohemian May 20 '23 at 11:11
  • You can group hyphens with optional digits and shorten your expression to something like `(?:-\d*){2}[-.]`. – oriberu May 20 '23 at 16:27
  • Thank you, Bohemian, for pointing that out. I should have put [-.] and, yes, the bracket around \d is superfluous. – Tony Vaughan May 21 '23 at 17:45
  • The regex needs to allow three hyphens separated by nothing or integers or two hyphens and a point at the end of the file name separated by nothing or integers. – Tony Vaughan May 21 '23 at 17:45

2 Answers2

1

Keeping it simple, you could use this pattern:

^.*(?:-[^-]*){2}-.*\.\w+$

This matches:

  • ^ from the start of the filename
    • .* match optional leading portion
    • (?:-[^-]*){2} match two hyphen separated components
    • - match a required third hyphen
    • .* followed by any other content
    • \.\w+ ending with dot and an extension
  • $ end of the filename

Demo

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
1

Looking at your pattern, you want to match either a hyphen or a dot with [-|.] which you can write as [-.] without the pipe (it would match the pipe literally if it is there)

Matching 3 hyphens separated by either a dot or hyphen might be shortened to:

-(?:\d*[-.]){2}

Explanation

  • - Match literally
  • (?: Non capture group
    • \d* match optional digits
    • [-.] Match either - or .
  • ){2} Close the non capture group and repeat it 2 times

Regex demo

If the whole pattern should end on .ts then:

.*(?:-\d*){2}(?=[.-]).*\.ts$

Or a bit more broader match:

.*(?:-\d*){2}(?=[.-]).*\.[^\s.]+$

Regex demo

Edit

If the third occurrence can only by a hyphen or dot:

-\d*-\d*[.-]

Regex demo

The fourth bird
  • 154,723
  • 16
  • 55
  • 70