Reputation: 45
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?
Upvotes: 3
Views: 47
Reputation: 627607
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).Upvotes: 2