Tuesday, 1 October 2013

Matching specific Apache VirtualHost with regex

Matching specific Apache VirtualHost with regex

I am currently attempting to match a single Apache VirtualHost entry
within an apache config file by the ServerName of the VirtualHost. I am
doing this with python, using the re module.
Here's an example of the type of file that I am working with:
<VirtualHost *:8000>
DocumentRoot "/path/to/dir1"
ServerName eg1.dev
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/path/to/docroot"
ServerName desired.dev
</VirtualHost>
<VirtualHost *:8080>
DocumentRoot "/path/to/dir3"
ServerName eg2.dev
</VirtualHost>


I want to match the VirtualHost with a ServerName of desired.dev. So the
match I am aiming for is:
<VirtualHost *:80>
DocumentRoot "/path/to/docroot"
ServerName desired.dev
</VirtualHost>


I assumed this would be fairly simple with a regex but I can't figure out
how to do it. I currently have:
<VirtualHost \*:[0-9]*>.*?ServerName +desired.dev.*?</VirtualHost>
But the issue with this being that it matches the very first
<VirtualHost..., matches the ServerName and stops matching at the end of
the closing tag of the desired VirtualHost.
I'm aware that there are probably python modules for parsing Apache config
files but I do not wish to use anything from outside of the standard
library. If attempting this with regex is a terrible idea, are there any
alternatives?

No comments:

Post a Comment