Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple Regex: match everything until the last dot

Tags:

regex

Just want to match every character up to but not including the last period

 dog.jpg     -> dog
 abc123.jpg.jpg -> abc123.jpg

I have tried

(.+?)\.[^\.]+$
like image 454
bigdowg Avatar asked Apr 22 '16 18:04

bigdowg


2 Answers

Use lookahead to assert the last dot character:

.*(?=\.)

Live demo.

like image 66
Quinn Avatar answered Oct 11 '22 12:10

Quinn


This will do the trick

(.*)\.

Regex Demo

The first captured group contains the name. You can access it as $1 or \1 as per your language

like image 33
rock321987 Avatar answered Oct 11 '22 12:10

rock321987