Tuesday, February 16, 2010

Programmer - Problem with look behind assertion and optional substring

Programmer Question

I'm trying to write some regex that will parse information from alerts generated by Hyperic HQ. The alerts come in as emails with a subject line like:



"[HQ] !!! - Alert: My Demo Website Alert Resource: demo.myserver.net Apache Web Server State: fixed"



To cut a very long story short, I need to be able to consistently grab the "Apache Web Server" part, regardless of the hostname which may not even be present. I do know that the hostname will always end "myserver.net" though.



The regex I have is:



/Resource:\s.*(?<=mydomain.net)?\s(.*)\s(?=State)/


I was expecting that this would match zero or more characters between "Resource:" and "State:", optionally following (but not including) a hostname.



Unfortunately, what it returns is "Server", i.e. the last word of the bit I want to match. This happens regardless of whether the hostname is in the string.



Can anyone help?

No comments:

Post a Comment

LinkWithin

Related Posts with Thumbnails