RegEx: capture words in double quotes

The last few months, I’ve been reading O’reilly’s Regular Expression book and was wondering how to capture text within a double quote such as: The “Dog” jumped over the “lazy” brown fox.

The regex I made up was something like this: “[^”](.*)” but this was the wrong solution. I tried a few other combination but all failed. Today I asked the same co-worker as the previous posting and he gave me: “([^”]+)” which gave me the result I needed.

Regex; Getting Dates

I’m recently reading up on Regular Expression and was having some issue getting the data I need. I have a html text file that has the following as its content:

<tbody>
<tr><td class="n"><a href="../">Parent Directory</a>/</td><td class="m"></td><td class="s">- </td><td class="t">Directory</td></tr>
<tr><td class="n"><a href="20091103/">20091103</a>/</td><td class="m">2009-Dec-29 19:31:07</td><td class="s">- </td><td class="t">Directory</td></tr>
<tr><td class="n"><a href="20100116/">20100116</a>/</td><td class="m">2010-Jan-30 01:54:24</td><td class="s">- </td><td class="t">Directory</td></tr>
<tr><td class="n"><a href="20100130/">20100130</a>/</td><td class="m">2010-Mar-26 05:31:56</td><td class="s">- </td><td class="t">Directory</td></tr>
<tr><td class="n"><a href="20100730/">20100730</a>/</td><td class="m">2010-Aug-08 15:59:47</td><td class="s">- </td><td class="t">Directory</td></tr>
<tr><td class="n"><a href="latest/">latest</a>/</td><td class="m">2010-Aug-05 03:46:25</td><td class="s">- </td><td class="t">Directory</td></tr>
</tbody>

The only part of this html file I need is the YYYYMMDD section between between <a href=”…”></a>. I tried a few ways of stripping out the desire date as using Powershell’s -match but I found that -match has its own shortcoming and was frustrating the hell out of me. I went to a co-worker of mine who excel in regular expression and he threw my regex syntax into Regulator and it would give me the result I want, so I know my criteria is correct but Powershell doesn’t like it or not showing me what I want.

Thus, I ended up using the .Net regex class and was able to get my desire result:

$c = gc ".Desktoppowershelllocal_site.txt"
([regex]'<as+href="d+/?">(d+)</a>').matches($c) | foreach {$_.Groups[1].Value}

Without the foreach loop, you will get a listing that look like this:


Groups   : {20091103, 20091103}
Success  : True
Captures : {20091103}
Index    : 1301
Length   : 32
Value    : 20091103

...
...

Change Windows Service Password

In my environment, I have over 100+ servers and every so many days, the account that runs SQL services are changed and it is a bitch to change it via point and click on 100+ servers. The script first look at the local machine and return services that has the [log on as] that matches PHX. It will then go into a foreach loop and change the password for only those services. The win32_service wmi has a [-computer] option, in which, you can use to access remote server.

$services = Get-WmiObject win32_service | where {$_.StartName -match '^PHX\*'}
$pwd = "pwd_here"

if ($matches[0] -ne $null)
{
    foreach ($service.Name in $services)
    {
        write-host $service.Name
        $service.Change($null,$null,$null,$null,$null,$null,$null,$pwd,$null,$null,$null)
    }
}

if there are no matches, the script will bomb.