27

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.

2
  • are you trying to match any filename at the end of a path (because that's what your regex attempts to do) or a filename that follows the 'XtXX.jpg' pattern? Commented Nov 8, 2011 at 2:05
  • @Dmitry: I'm really trying to find the regex that would match in general the filename at the end. Commented Nov 8, 2011 at 2:06

6 Answers 6

52

Here's a simple Regex that will match everything after the last /:

/[^/]*$
Sign up to request clarification or add additional context in comments.

Comments

30

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

/\/\dt\d\d\.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.

8 Comments

Yes, that last thing is what I really wanted. The other works but this last one I can learn something from :).
@jfriend00 Not an answer for "Regex that matches the last occurrence of a character".
@RamtinSoltani - Well it turns out that isn't exactly what the OP wanted. The OP wanted to match the filename at the end of the URL starting at the last / and I provided that in my second answer. If you follow what the OP said in their comments, it is quite clear this is the full solution they wanted. I can't help it if their title wasn't perfectly descriptive or if you thought you were going to get an answer purely based on their title. That isn't how things work here. We often have to have a conversation with the OP and figure out what they really need and that's what was done here.
@RamtinSoltani - Oh, and there is no regex for matching just the last occurrence of a character. You can match starting with some char to the end with no other occurrences of that character (which means you implicitly started at the last occurrence) and that's what I provided and is what the OP needed. Geez, can't believe you downvoted the selected answer that provided the OP with exactly what they wanted. Read the OP's comments here on my answer and on their own question.
Yes, but when everything works out, you should adjust the question, otherwise it would be confusing. This question will show up in search engines based on title (that's how I found it), while the answer (even though it's correct) is not related to the original question. So why not adjust the question with an edit (since you're a stackoverflow pro) to avoid confusing others?
|
9

As Johnsyweb says, a regular express isn't really needed here. AFAIK the fastest way to do this is with lastIndexOf and substr.

str.substr(str.lastIndexOf('/'));

1 Comment

4

Of course you don't have to use a regular expression to split a string and pop the last part:

var str="http://img.oo.com.au/prod/CRWWBGFWG/1t44.jpg";
var parts = str.split("/");
document.write(parts.pop() + "<br />");

Comments

0

Based on answer of Scott, try this: (JavaScript)

var url = "http://img.oo.com.au/prod/CRWWBGFWG/1t44.jpg";

var path           = url.replace(/(.*)([\\\/][^\\\/]*$)/, "$1" );

var lastElement    = url.replace(/(.*)([\\\/][^\\\/]*$)/, "$2" );

This can be also matched for Windows/Nix file path, to extract file name or file path :

c:\Program Files\test.js => c:\Program Files

c:\Program Files\test.js => \test.js

Comments

0

This is for Java on a Linux machine. It grabs the last part of a file path, so that it can be used for making a file lock.

// String to be scanned to find the pattern.
String pattern = ".*?([^/.]+)*$";

// Create a Pattern object
Pattern r = Pattern.compile(pattern);

// Now create matcher object.
Matcher match = r.matcher("/usr/local/java/bin/keystore");

/**
* Now you have two matches
* #0    /usr/local/java/bin/keystore
* #1    keystore
*/

String fileLock = "";

if (match.find()) {
    fileLock = match.group(1) + ".lock";
}

A little different than the original question, I know. But I hope this helps others who were stuck with the same problem I had.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.