2

The following regex

var re = /(?<=_)\d*/g

extracts the bitrate from filenames that follow a naming convention like filename_bitrate.ext

It works for most filenames -

asdfasd_400.mp4
asdfasd_600.mp3
asdfasd_1000.mxf

Some files follow the same convention but my regex can't parse them. Ex -

BLIVE_CEOFoC_210720_1245_400.mp4

I want to basically extract the number before the .ext. How do I modify my regex to work with any filename?

  • Does this need to use regex? If you can use split it's more readable and equally effective – ᴓᴓᴓ Jul 20 '21 at 20:14
  • That is a pretty unreliable way to get the bitrate. You might want to look at [tag:ffmpeg], examples [here](https://stackoverflow.com/q/43263575/3124333) and [here](https://superuser.com/q/1106343/1095685). – SiKing Jul 20 '21 at 20:19

1 Answers1

1

You can use

(?<=_)\d+(?=\.\w+$)
(?<=_)\d+(?=\.[^.]+$)

See the regex demo. Details:

  • (?<=_) - a positive lookbehind that matches a location immediately preceded with a _
  • \d+ - one or more digits
  • (?=\.\w+$) - a positive lookahead that matches a location immediately followed with a dot and one or more word chars till the end of string. If you use the second version with [^.]+, it will match any one or more chars other than a dot (so, allowing any chars in the extension).
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563