Link to home
Start Free TrialLog in
Avatar of crysallus
crysallusFlag for Australia

asked on

Regex to find relative filenames in html code.


Firstly, here's my main question. How do I define a regex to extract relative file names from html code?

If that interests you, here's my problem...

I'm developing a program that manages templating of html files, and I need a way to handle relative filenames within the html. For example, file C:\dir\index.html acts as a template file, containing html code to be used across other html files that depend upon it, eg. C:\dir\subdir\index.html. Now most of the html code can simply be copied across as is, however any relative filenames within C:\dir\index.html, will obviously no longer point to the right place if copied directly into C:\dir\subdir\index.html. So, I need to adjust any relative file names to suit the new file location.

The current approach I'm working on is to use a regex to identify any filenames in the original html, then in the program code (C#), check that the retrieved file name exists and is relative. This is because I don't trust my regex skills to get this right perfectly, so I'm designing the regex to capture anything that might be a filename, and then double check it in the code.

Currently my regex looks like this:


which seems to capture any filename or url so far in testing.

A few criteria:
- needs to handle a filename anywhere, not just neatly embedded within single or double quotes
- needs to cope with filenames with embedded white space that isn't a part of the file name (eg. file name across multiple lines, which, I think, is valid in html)
- can assume that the file has an extension (which is what I've done above)
- needs to capture the whole filename and path as written in the html, but nothing else
- can be a little too inclusive, as long as anything that it detects that isn't a relative file path can be easily checked for in C# code (eg. using Path.IsPathRooted)

If anyone has any better regex's, or better solutions to my problem, that would be helpful.

Avatar of Terry Woods
Terry Woods
Flag of New Zealand image

Can you provide some example data to work with please?
Avatar of crysallus


Well, it needs to work with any html, not any specific html, so I've just been doing view page source to get test data. Including on this website.

An update on my attempt above. After further testing I've now got:

Avatar of Terry Woods
Terry Woods
Flag of New Zealand image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I tried yours, but it didn't work for me.

I've been testing using, and it doesn't seem to match anything.

Small typo perhaps?

Yeah, the point about requiring the file extension is a good one, which I am aware of. I'm just not sure how else to detect a file name in a manner that distinguishes it from the rest of the html otherwise. As you said, that would probably require knowing all possible contexts (i.e. tag attributes) within which filenames could be found. Problem is, I'm not sure exactly what all those contexts would be (others may though), and that wouldn't handle filenames within javascript embedded in the html either.

Also, the sorts of filenames that I'm typically thinking of are image, or css, or js files, which would be written with their extension. Relative links to other urls however, may not, which may be an issue.
Works for me in that tool once the ignore case option is checked.
Ah, I see.

Would it be safe to assume that any link that doesn't include the extension and relies on URL rewriting would end with '/'? From what I've seen, they tend to, but I'm guessing that isn't necessarily the case.

I amended yours to:


which does pick up filenames that end with /, as well as ones with extensions, but it also matches with the // at the start of a comment in javascript. Any way around that?
> Would it be safe to assume that any link that doesn't include the extension and relies on URL rewriting would end with '/'?

No, rewritten URLs can be anything, essentially.

This when run in multiline (and ignore case) mode will exclude javascript comments where they occur at the start of a line (can have leading spaces). Because the result array will be a slightly different structure, you'll need to process the results differently.
I've actually gone with an expanded regex building on your suggestions, but thanks for the input, it definitely helped.