Windows Vista Forums
Vista Forums Home Join Vista Forums Webcasts Windows 7 Forum Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

Results from Regular Expression Match

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 05-20-2008   #1 (permalink)
Mike
Guest


 

Results from Regular Expression Match

All,

I have the following script in file parser.ps1.

$a = get-content "C:\__Junk\ParseLuts\2008.05.01.log"
foreach ($i in $a)
{
$regex = [regex] "(?<= AE).*(?= on Host)"
$re = $regex.match($i)
$re
}

Upon running this I get the following back when the match is true:

Groups : { SMHMASTERA_SCU}
Success : True
Captures : { SMHMASTERA_SCU}
Index : 14
Length : 15
Value : SMHMASTERA_SCU

I only want to redirect the results for "Value" into a file. How can I just
get the Value of "Value" and redirect into a file rather than everything from
Groups to Length?

Thanks from this newbie.

Mike




My System SpecsSystem Spec
Old 05-20-2008   #2 (permalink)
Keith Hill [MVP]
Guest


 

Re: Results from Regular Expression Match

"Mike" <Mike@xxxxxx> wrote in message
news:6179FCE3-31C3-4D91-87CF-E560F23FE7A2@xxxxxx
Quote:

> All,
>
> I have the following script in file parser.ps1.
>
> $a = get-content "C:\__Junk\ParseLuts\2008.05.01.log"
> foreach ($i in $a)
> {
> $regex = [regex] "(?<= AE).*(?= on Host)"
> $re = $regex.match($i)
> $re
> }
>
> Upon running this I get the following back when the match is true:
>
> Groups : { SMHMASTERA_SCU}
> Success : True
> Captures : { SMHMASTERA_SCU}
> Index : 14
> Length : 15
> Value : SMHMASTERA_SCU
>
> I only want to redirect the results for "Value" into a file. How can I
> just
> get the Value of "Value" and redirect into a file rather than everything
> from
> Groups to Length?
>
Change the last $re to:

$re.Value >> <path to file>

BTW you can also accomplish this a bit more succinctly by using switch like
so:

switch -regex -file C:\__Junk\ParseLuts\200805.01.log {
'(?<= AE).*(?= on Host)' { $matches[1] >> <path to file> }
}

--
Keith

My System SpecsSystem Spec
Old 05-20-2008   #3 (permalink)
Kiron
Guest


 

Re: Results from Regular Expression Match

Here are a some more ideas:

# some global variables
$log = 'C:\__Junk\ParseLuts\2008.05.01.log'
$regex = [regex]'(?<= AE).*(?= on Host)'
$a = gc $log

# 1 - original modified
$values1 = $(foreach ($i in $a) {
$re = $regex.match($i)
if ($re.success) {$re.value} # avoid blanks
})

# 2 - using Switch (Keith's has a bug)
$values2 = $(switch -r -f $log {$regex {$matches[0]}})

# 3 - using Select-String
$values3 = $(select-string $regex $log |
% {$_.matches} | % {$_.value})

# 4 - using the Matches Method
$values4 = $regex.matches(($a | out-string)) | % {$_.value}

# write to file
sc .\out.txt $values4

gc .\out.txt

--
Kiron
My System SpecsSystem Spec
Closed Thread

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
regular expression help Stimp VB Script 9 08-02-2008 06:05 PM
Simple Regular Expression BJ PowerShell 6 01-30-2008 02:46 PM
Re: regular expression howto Keith Hill [MVP] PowerShell 0 01-08-2008 09:45 PM
Extracting regular expression match from file Brian Vallelunga PowerShell 2 06-18-2007 02:41 AM
simple regular expression Ben PowerShell 2 02-19-2007 08:28 AM


Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media 2005-2008

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51