Domanda

I've been banging my head against a wall trying to work out the Regex for an IIS Rewrite rule - which should match the following URL:

/path1/path2/staticword/dynamic/dynamic2/dynamic3/hello.css

I need it to extract the path before staticword, so path1/path2/

I then need it to match the file name hello.css

Note: this must only trigger when there is a file extension (.css |.js)

I do not need the middle section.

So far I have got this:

([\s\S]*)staticword([\s\S]*)(.css|.js)

This gets me the first part - but not the filename. I fear I need a negative look-behind - as we cannot be sure of how many paths there are.

Thanks

È stato utile?

Soluzione

You were very close to the correct solution! You just needed to add a slash (/) so that the greedy [\s\S]* matcher after the staticword captures everything up to the final slash, leaving you free to capture the filename next.

Try the following regex:

([\s\S]*?)staticword[\s\S]*/([\s\S]*?(.css|.js))

It will capture the following groups:

match[0] = /path1/path2/staticword/dynamic/dynamic2/dynamic3/hello.css
match[1] = /path1/path2/
match[2] = hello.css
match[3] = .css

Caveat: due to the greedy matching, you will need to test one path at a time

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top