Arno
Arno

Reputation: 329

Match a regex from characters to end of line but ignore the last character

The regex

(file\..*$)

matches "file.xml" correctly in

$cd = "..\folder\file.xml"

but I'd like to ignore the trailing quote via regex if possible

Upvotes: 2

Views: 2229

Answers (3)

Dmitry Egorov
Dmitry Egorov

Reputation: 9650

Move the last char along with the end marker out of the group:

(file\..*).$

https://regex101.com/r/bZ7vV5/1

Or if you may have a non-quote last char which you would like to capture instead, then specify this quote explicitly but as optional ("?) and make the previous match non-greedy (.*?):

(file\..*?)"?$

https://regex101.com/r/bZ7vV5/3

Upvotes: 0

José Castro
José Castro

Reputation: 671

You can update your regular expression to the following:

(file\..*?)"?$

That'll make sure that if there is a trailing quote it will not be captured.

Upvotes: 1

Jan
Jan

Reputation: 43189

You could say: anything not a ":

(file\.[^"]*)"$

See a demo on regex101.com.


In your example, you do not even need the anchors:

(file\.[^"]*)

Upvotes: 1

Related Questions