tags:

views:

163

answers:

4

I have two example filename strings:

jquery.ui.min.js
jquery.ui.min.css

What regex can I use to only match the LAST dot? I don't need anything else, just the final dot.

A little more on what I'm doing. I'm using PHP's preg_split() function to split the filename into an array. The function deletes any matches and gives you an array with the elements between splits. I'm trying to get it to split jquery.ui.min.js into an array that looks like this:

array[0] = jquery.ui.min
array[1] = js
+1  A: 
\.[^.]*$
Brandon Horsley
+3  A: 

If you're looking to extract the last part of the string, you'd need:

\.([^.]*)$

if you don't want the . or

(\.[^.]*)$

if you do.

paxdiablo
+1  A: 

I think you'll have a hard time using preg_split, preg_match should be the better choice.

preg_match('/(.*)\.([^.]*)$/', $filename, $matches);

Alternatively, have a look at pathinfo.
Or, do it very simply in two lines:

$filename = substr($file, 0, strrpos($file, '.'));
$extension = substr($file, strrpos($file, '.') + 1);
deceze
Cool! pathinfo() does exactly what I needed. Thank you!
Marshall Thompson
agreed. There is no reason regex needs to be used for this.
Crayon Violent
+2  A: 

At face value there is no reason to use regex for this. Here are 2 different methods that use functions optimized for static string parsing:

Option 1:

$ext = "jquery.ui.min.css";
$ext = array_pop(explode('.',$ext));
echo $ext;

Option 2:

$ext = "jquery.ui.min.css";
$ext = pathinfo($ext);
echo $ext['extension'];
Crayon Violent