Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex - How to replace period in file name with an underscore

Tags:

regex

I'm looking to build a regular expression that will allow me to replace the periods (.) in a filename with an underscore (_).

Example: replace filename.doc with filename_doc

I've had a play around in Expresso, and have worked out that it will be something like .\w{3,4) - to pick up 3 or 4 character extentions eg doc, docx, xls, xlsx etc.

But I cannot figure out how to replace only the period and retain the extention.

like image 426
Sean Taylor Avatar asked Dec 04 '25 09:12

Sean Taylor


1 Answers

You have to escape . sign. Try with following regex:

\.(\w{3,4})$

And replace it with:

_$1

$1 contains matched string in ()

like image 170
hsz Avatar answered Dec 06 '25 06:12

hsz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!