john c. j.
john c. j.

Reputation: 1185

Regex to match parts of the filename with multiple extensions

I'm trying to write the regex to match

of the following string:

filename_with_multiple_exts.aaa.bbb.user.js

This is what I have currently (test):

(\.([^\.]*[\.])*)([^\.]+)$

But it isn't exactly what I'm searching for.

Upvotes: 1

Views: 1812

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627607

You may split the string at the first dot using

([^.]*)\.(.*)

See the regex demo. Anchors are not required, you might just want to tune the regex if you operate on individual lines, if not, it will work as is.

Details

  • ([^.]*) - Group 1: any 0 or more chars other than dots
  • \. - a dot
  • (.*) - Group 2: any 0 or more char (usually, other than line breaks, but that varies from regex flavor to flavor).

See a visual graph, too:

enter image description here

Upvotes: 2

Related Questions