Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex: how to match the last dot in a string

Tags:

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 
like image 246
Marshall Thompson Avatar asked Jul 26 '10 02:07

Marshall Thompson


People also ask

How do you match a dot in regex?

in regex is a metacharacter, it is used to match any character. To match a literal dot in a raw Python string ( r"" or r'' ), you need to escape it, so r"\." Unless the regular expression is stored inside a regular python string, in which case you need to use a double \ ( \\ ) instead.

How do you match the end of a string?

End of String or Line: $ The $ anchor specifies that the preceding pattern must occur at the end of the input string, or before \n at the end of the input string. If you use $ with the RegexOptions. Multiline option, the match can also occur at the end of a line.

Does regex match dot space?

Yes, the dot regex matches whitespace characters when using Python's re module.


1 Answers

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.

like image 116
paxdiablo Avatar answered Sep 21 '22 05:09

paxdiablo