Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Finding a regex position in a string

Reply
 
Old 11-26-2007   #1 (permalink)
Jacob Saaby Nielsen


 
 

Finding a regex position in a string

Hey again ;o)

Doing a script. Using a regex to find the various bits and pieces of each
line of text I run through. So I have
a regex for the time, the date, etc.

Question is, is there a property to the regex, kinda like $regex.ismatch
but perhaps $regex.position, that tells
me at which character in the string, the positive match begins ? That would
make life a lot easier for me, with
regards to getting the text out of the string

Best Regards,
Jacob



My System SpecsSystem Spec
Old 11-26-2007   #2 (permalink)
Shay Levi


 
 

Re: Finding a regex position in a string


Check the Index and the Length properties

PS > [regex]::match("powershell rocks!","shell")


Groups : {shell}
Success : True
Captures : {shell}
Index : 5
Length : 5
Value : shell



-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com


Quote:

> Hey again ;o)
>
> Doing a script. Using a regex to find the various bits and pieces of
> each
> line of text I run through. So I have
> a regex for the time, the date, etc.
> Question is, is there a property to the regex, kinda like
> $regex.ismatch
> but perhaps $regex.position, that tells
> me at which character in the string, the positive match begins ? That
> would
> make life a lot easier for me, with
> regards to getting the text out of the string
> Best Regards,
> Jacob

My System SpecsSystem Spec
Old 11-26-2007   #3 (permalink)
Jacob Saaby Nielsen


 
 

Re: Finding a regex position in a string

Hello Shay,

thanks man, just what I needed


Best Regards,
Jacob Saaby Nielsen
mailto:jacob.saaby@xxxxxx
Quote:

> Check the Index and the Length properties
>
PS>> [regex]::match("powershell rocks!","shell")
PS>>
Quote:

> Groups : {shell}
> Success : True
> Captures : {shell}
> Index : 5
> Length : 5
> Value : shell
> -----
> Shay Levi
> $cript Fanatic
> http://scriptolog.blogspot.com
Quote:

>> Hey again ;o)
>>
>> Doing a script. Using a regex to find the various bits and pieces of
>> each
>> line of text I run through. So I have
>> a regex for the time, the date, etc.
>> Question is, is there a property to the regex, kinda like
>> $regex.ismatch
>> but perhaps $regex.position, that tells
>> me at which character in the string, the positive match begins ? That
>> would
>> make life a lot easier for me, with
>> regards to getting the text out of the string
>> Best Regards,
>> Jacob

My System SpecsSystem Spec
Old 11-26-2007   #4 (permalink)
Jacob Saaby Nielsen


 
 

Re: Finding a regex position in a string

Hey Shay,

so I pass this string to my function:

[string]$testString = "2007-11-01 10:14:15 Local7.Notice 192.168.3.253 56382
11/01/2007 10:15:53.400 SEV=4 AUTH/22 RPT=1784 User [domain\user] Group
[group] connected, Session Type: IPSec"

And this is my function:

function getTime ([string]$UserTime)
{
# Look for time in a string
# Regular Expression: \d{2}[:]\d{2}[:]\d{2}

$Regex = [regex]"\d{2}[:]\d{2}[:]\d{2}"

Write-Host $Regex($UserTime).Value

}

That just writes the contents of $regex. Originally I did

Write-Host $Regex.Value($UserTime)

That gave me an error, that Value isn't a property that exists.

Can anyone help me out here, and point me in the right direction ? I need
to use the regex pattern to find
whatever I need in the string, then be able to at least get the index of
the match. Preferably the length also,
and it would be way cool to get the Value of the regex match straight away.

So if you got the knowledge everyone, share it ! (Please )

Best Regards,
Jacob Saaby Nielsen
mailto:jacob.saaby@xxxxxx
Quote:

> Check the Index and the Length properties
>
PS>> [regex]::match("powershell rocks!","shell")
PS>>
Quote:

> Groups : {shell}
> Success : True
> Captures : {shell}
> Index : 5
> Length : 5
> Value : shell
> -----
> Shay Levi
> $cript Fanatic
> http://scriptolog.blogspot.com
Quote:

>> Hey again ;o)
>>
>> Doing a script. Using a regex to find the various bits and pieces of
>> each
>> line of text I run through. So I have
>> a regex for the time, the date, etc.
>> Question is, is there a property to the regex, kinda like
>> $regex.ismatch
>> but perhaps $regex.position, that tells
>> me at which character in the string, the positive match begins ? That
>> would
>> make life a lot easier for me, with
>> regards to getting the text out of the string
>> Best Regards,
>> Jacob

My System SpecsSystem Spec
Old 11-26-2007   #5 (permalink)
Shay Levi


 
 

Re: Finding a regex position in a string

I changed your code a bit to perform named group lookups:

[string]$testString = "2007-11-01 10:14:15 Local7.Notice 192.168.3.253 56382
11/01/2007 10:15:53.400 SEV=4 AUTH/22 RPT=1784 User [domain\user] Group [group]
connected, Session Type: IPSec"
$Regex = [regex]'(?<userTime>\d{2}:\d{2}:\d{2})'
$Regex.matches($testString)

Groups : {10:14:15, 10:14:15}
Success : True
Captures : {10:14:15}
Index : 11
Length : 8
Value : 10:14:15

Groups : {10:15:53, 10:15:53}
Success : True
Captures : {10:15:53}
Index : 65
Length : 8
Value : 10:15:53



Now you can get the matches by pointing to the group name:


PS > $Regex.matches($testString) | foreach {$_.groups['userTime'].value}
10:14:15
10:15:53

PS > $Regex.matches($testString) | foreach {$_.groups['userTime'].Index}
11
65


There is also a built-in help file for regular exprresions in PowerShell:

PS > help about_regular_expression


Some more resources:

Keith Hill's blog
Effective PowerShell Item 9: Regular Expressions - One of the Power Tools
in PowerShell
http://keithhill.spaces.live.com/Blo...3A97!820.entry


The 30 Minute Regex Tutorial
http://www.codeproject.com/dotnet/RegexTutorial.asp


Windows PowerShell: The Definitive Guide Guide
Appendix B. Regular Expression Reference
http://safari.oreilly.com/9780596528...sion_reference


-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com


Quote:

> Hey Shay,
>
> so I pass this string to my function:
>
> [string]$testString = "2007-11-01 10:14:15 Local7.Notice 192.168.3.253
> 56382 11/01/2007 10:15:53.400 SEV=4 AUTH/22 RPT=1784 User
> [domain\user] Group [group] connected, Session Type: IPSec"
>
> And this is my function:
>
> function getTime ([string]$UserTime)
> {
> # Look for time in a string
> # Regular Expression: \d{2}[:]\d{2}[:]\d{2}
> $Regex = [regex]"\d{2}[:]\d{2}[:]\d{2}"
>
> Write-Host $Regex($UserTime).Value
>
> }
>
> That just writes the contents of $regex. Originally I did
>
> Write-Host $Regex.Value($UserTime)
>
> That gave me an error, that Value isn't a property that exists.
>
> Can anyone help me out here, and point me in the right direction ? I
> need
> to use the regex pattern to find
> whatever I need in the string, then be able to at least get the index
> of
> the match. Preferably the length also,
> and it would be way cool to get the Value of the regex match straight
> away.
> So if you got the knowledge everyone, share it ! (Please )
>
> Best Regards,
> Jacob Saaby Nielsen
> mailto:jacob.saaby@xxxxxx
Quote:

>> Check the Index and the Length properties
>>
PS>>> [regex]::match("powershell rocks!","shell")
PS>>>
Quote:
Quote:

>> Groups : {shell}
>> Success : True
>> Captures : {shell}
>> Index : 5
>> Length : 5
>> Value : shell
>> -----
>> Shay Levi
>> $cript Fanatic
>> http://scriptolog.blogspot.com
Quote:

>>> Hey again ;o)
>>>
>>> Doing a script. Using a regex to find the various bits and pieces of
>>> each
>>> line of text I run through. So I have
>>> a regex for the time, the date, etc.
>>> Question is, is there a property to the regex, kinda like
>>> $regex.ismatch
>>> but perhaps $regex.position, that tells
>>> me at which character in the string, the positive match begins ?
>>> That
>>> would
>>> make life a lot easier for me, with
>>> regards to getting the text out of the string
>>> Best Regards,
>>> Jacob

My System SpecsSystem Spec
Old 11-26-2007   #6 (permalink)
Jeff


 
 

Re: Finding a regex position in a string

On Nov 26, 5:01 pm, Jacob Saaby Nielsen <jacob.sa...@xxxxxx>
wrote:
Quote:

> Hey Shay,
>
> so I pass this string to my function:
>
> [string]$testString = "2007-11-01 10:14:15 Local7.Notice 192.168.3.253 56382
> 11/01/2007 10:15:53.400 SEV=4 AUTH/22 RPT=1784 User [domain\user] Group
> [group] connected, Session Type: IPSec"
>
> And this is my function:
>
> function getTime ([string]$UserTime)
> {
> # Look for time in a string
> # Regular Expression: \d{2}[:]\d{2}[:]\d{2}
>
> $Regex = [regex]"\d{2}[:]\d{2}[:]\d{2}"
>
> Write-Host $Regex($UserTime).Value
>
> }
>
> That just writes the contents of $regex. Originally I did
>
> Write-Host $Regex.Value($UserTime)
>
> That gave me an error, that Value isn't a property that exists.
>
> Can anyone help me out here, and point me in the right direction ? I need
> to use the regex pattern to find
> whatever I need in the string, then be able to at least get the index of
> the match. Preferably the length also,
> and it would be way cool to get the Value of the regex match straight away.
>
> So if you got the knowledge everyone, share it ! (Please )
>
> Best Regards,
> Jacob Saaby Nielsen
> mailto:jacob.sa...@xxxxxx
>
Quote:

> > Check the Index and the Length properties
>
> PS>> [regex]::match("powershell rocks!","shell")
> PS>>
>
Quote:

> > Groups : {shell}
> > Success : True
> > Captures : {shell}
> > Index : 5
> > Length : 5
> > Value : shell
> > -----
> > Shay Levi
> > $cript Fanatic
> >http://scriptolog.blogspot.com
Quote:

> >> Hey again ;o)
>
Quote:
Quote:

> >> Doing a script. Using a regex to find the various bits and pieces of
> >> each
> >> line of text I run through. So I have
> >> a regex for the time, the date, etc.
> >> Question is, is there a property to the regex, kinda like
> >> $regex.ismatch
> >> but perhaps $regex.position, that tells
> >> me at which character in the string, the positive match begins ? That
> >> would
> >> make life a lot easier for me, with
> >> regards to getting the text out of the string
> >> Best Regards,
> >> Jacob
Jacob,

You need to work with a Match object to get what you need:

$match = $Regex.Match( $UserTime )

# the following properties get you the index and length,
# but you can use the Value property if you just need
# the value of the matched text

# Write-Host $match.Index
# Write-Host $match.Length

Write-Host $match.Value

I hope this helps.

Jeff
My System SpecsSystem Spec
Old 11-26-2007   #7 (permalink)
Jacob Saaby Nielsen


 
 

Re: Finding a regex position in a string

Hey Jeff and Shay,

awesome both of you ! Thanks a lot, appreciate it

Best Regards,
Jacob Saaby Nielsen
mailto:jacob.saaby@xxxxxx
Quote:

> On Nov 26, 5:01 pm, Jacob Saaby Nielsen <jacob.sa...@xxxxxx>
> wrote:
>
Quote:

>> Hey Shay,
>>
>> so I pass this string to my function:
>>
>> [string]$testString = "2007-11-01 10:14:15 Local7.Notice
>> 192.168.3.253 56382
>> 11/01/2007 10:15:53.400 SEV=4 AUTH/22 RPT=1784 User [domain\user]
>> Group
>> [group] connected, Session Type: IPSec"
>> And this is my function:
>>
>> function getTime ([string]$UserTime)
>> {
>> # Look for time in a string
>> # Regular Expression: \d{2}[:]\d{2}[:]\d{2}
>> $Regex = [regex]"\d{2}[:]\d{2}[:]\d{2}"
>>
>> Write-Host $Regex($UserTime).Value
>>
>> }
>>
>> That just writes the contents of $regex. Originally I did
>>
>> Write-Host $Regex.Value($UserTime)
>>
>> That gave me an error, that Value isn't a property that exists.
>>
>> Can anyone help me out here, and point me in the right direction ? I
>> need
>> to use the regex pattern to find
>> whatever I need in the string, then be able to at least get the index
>> of
>> the match. Preferably the length also,
>> and it would be way cool to get the Value of the regex match straight
>> away.
>> So if you got the knowledge everyone, share it ! (Please )
>>
>> Best Regards,
>> Jacob Saaby Nielsen
>> mailto:jacob.sa...@xxxxxx
Quote:

>>> Check the Index and the Length properties
>>>
>> PS>> [regex]::match("powershell rocks!","shell")
>> PS>>
Quote:

>>> Groups : {shell}
>>> Success : True
>>> Captures : {shell}
>>> Index : 5
>>> Length : 5
>>> Value : shell
>>> -----
>>> Shay Levi
>>> $cript Fanatic
>>> http://scriptolog.blogspot.com
>>>> Hey again ;o)
>>>>
>>>> Doing a script. Using a regex to find the various bits and pieces
>>>> of
>>>> each
>>>> line of text I run through. So I have
>>>> a regex for the time, the date, etc.
>>>> Question is, is there a property to the regex, kinda like
>>>> $regex.ismatch
>>>> but perhaps $regex.position, that tells
>>>> me at which character in the string, the positive match begins ?
>>>> That
>>>> would
>>>> make life a lot easier for me, with
>>>> regards to getting the text out of the string
>>>> Best Regards,
>>>> Jacob
> Jacob,
>
> You need to work with a Match object to get what you need:
>
> $match = $Regex.Match( $UserTime )
>
> # the following properties get you the index and length,
> # but you can use the Value property if you just need
> # the value of the matched text
> # Write-Host $match.Index
> # Write-Host $match.Length
> Write-Host $match.Value
>
> I hope this helps.
>
> Jeff
>

My System SpecsSystem Spec
Old 11-26-2007   #8 (permalink)
Jacob Saaby Nielsen


 
 

Re: Finding a regex position in a string

Hey Jeff,

I ended up using the code you pointed out, and blogged it right here:

http://www.pipforhelvede.net/post/20...ing-%3bo).aspx

Best Regards,
Jacob Saaby Nielsen
mailto:jacob.saaby@xxxxxx
Quote:

> On Nov 26, 5:01 pm, Jacob Saaby Nielsen <jacob.sa...@xxxxxx>
> wrote:
>
Quote:

>> Hey Shay,
>>
>> so I pass this string to my function:
>>
>> [string]$testString = "2007-11-01 10:14:15 Local7.Notice
>> 192.168.3.253 56382
>> 11/01/2007 10:15:53.400 SEV=4 AUTH/22 RPT=1784 User [domain\user]
>> Group
>> [group] connected, Session Type: IPSec"
>> And this is my function:
>>
>> function getTime ([string]$UserTime)
>> {
>> # Look for time in a string
>> # Regular Expression: \d{2}[:]\d{2}[:]\d{2}
>> $Regex = [regex]"\d{2}[:]\d{2}[:]\d{2}"
>>
>> Write-Host $Regex($UserTime).Value
>>
>> }
>>
>> That just writes the contents of $regex. Originally I did
>>
>> Write-Host $Regex.Value($UserTime)
>>
>> That gave me an error, that Value isn't a property that exists.
>>
>> Can anyone help me out here, and point me in the right direction ? I
>> need
>> to use the regex pattern to find
>> whatever I need in the string, then be able to at least get the index
>> of
>> the match. Preferably the length also,
>> and it would be way cool to get the Value of the regex match straight
>> away.
>> So if you got the knowledge everyone, share it ! (Please )
>>
>> Best Regards,
>> Jacob Saaby Nielsen
>> mailto:jacob.sa...@xxxxxx
Quote:

>>> Check the Index and the Length properties
>>>
>> PS>> [regex]::match("powershell rocks!","shell")
>> PS>>
Quote:

>>> Groups : {shell}
>>> Success : True
>>> Captures : {shell}
>>> Index : 5
>>> Length : 5
>>> Value : shell
>>> -----
>>> Shay Levi
>>> $cript Fanatic
>>> http://scriptolog.blogspot.com
>>>> Hey again ;o)
>>>>
>>>> Doing a script. Using a regex to find the various bits and pieces
>>>> of
>>>> each
>>>> line of text I run through. So I have
>>>> a regex for the time, the date, etc.
>>>> Question is, is there a property to the regex, kinda like
>>>> $regex.ismatch
>>>> but perhaps $regex.position, that tells
>>>> me at which character in the string, the positive match begins ?
>>>> That
>>>> would
>>>> make life a lot easier for me, with
>>>> regards to getting the text out of the string
>>>> Best Regards,
>>>> Jacob
> Jacob,
>
> You need to work with a Match object to get what you need:
>
> $match = $Regex.Match( $UserTime )
>
> # the following properties get you the index and length,
> # but you can use the Value property if you just need
> # the value of the matched text
> # Write-Host $match.Index
> # Write-Host $match.Length
> Write-Host $match.Value
>
> I hope this helps.
>
> Jeff
>

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Get single char from string by position ? VB Script
regex to capitalize first letter in a specific position PowerShell
Re: how to tokenize a string into a variable via regex PowerShell
How export-csv deals with string versus string[] PowerShell
String PRODUCT_NAME was not found in string table Vista General


Vista Forums 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 Ltd

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