happymoniker
happymoniker

Reputation: 41

RegEx expression to select folder path of a file type

I am looking to make a Regex expression to find places where pdf files are referenced, and select their folder path.

For example for the expression "http://folders/../file.pdf" it should select "http://folders/../

The issue is that I only want it to select pdf files and escape if it hits a second quotation mark ("http://folders/other" doesn't have anything get selected)

I currently have two regex expressions that each do half of what I want:

"(.[^"]*?)(?=\.pdf") selects all the paths that end with a pdf, but includes the filename.

"(.[^"]*[\/]) properly grabs just the folder path, but does it for all links.

Is there a way I can get a regex expression that selects just the folder path only when the file ends with a .pdf extension?

Upvotes: 0

Views: 1994

Answers (1)

nu11p01n73R
nu11p01n73R

Reputation: 26667

A positive lookahead (?= ) would be usefull

"(.[^"]*[\/])(?=.*\.pdf"$)

see the example

http://regex101.com/r/gT6kI4/1

Explanation

"(.[^"]*[\/]) matches the path

(?=.*\.pdf"$) asserts that the path is followed by something (.*) and ends with .pdf

Upvotes: 2

Related Questions