Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
116
rated 0 times [  119] [ 3]  / answers: 1 / hits: 70127  / 13 Years ago, tue, november 8, 2011, 12:00:00

Having this text:



http://img.oo.com.au/prod/CRWWBGFWG/1t44.jpg


And other texts like this where the last 1 can be any other number and the last 44 can be any other number as well, I need a regex that will match /1t44.jpg.



Everything I've tried so far (/.+?.([^.]+)$) matches from the first slash (//img.oo.com.au/prod/CRWWBGFWG/1t44.jpg).



I'm using JavaScript, so whatever works on RegexPal should do.


More From » regex

 Answers
398

If you want to match a filename with a very specific file extenstion, you can use something like this:



//dtdd.jpg$/


This matches:




  • a slash

  • followed by a digit

  • followed by the letter 't'

  • followed by two digits

  • followed by '.jpg' at the end of the string



Or, if you really just want the filename (whatever is after the last slash with any file extension), then you can use this:



//[^/]+$/


This matches:




  • a slash

  • followed by one or more non-slash characters

  • at the end of the string



In your sample string of http://img.oo.com.au/prod/CRWWBGFWG/1t44.jpg, both of these will match /1t44.jpg. The first is obviously much more restrictive since it requires a specific format of the filename. The second matches any filename.






Other choices. In node.js development, you can use the path module and use path.parse() to break a path up into all of its various components.



And, there are various libraries written for the browser that will break up a path into its components too.


[#89250] Sunday, November 6, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
coleman

Total Points: 518
Total Questions: 81
Total Answers: 96

Location: Aland Islands
Member since Wed, Nov 17, 2021
3 Years ago
;