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 - New lines in regular expressions

Reply
 
Old 12-19-2007   #1 (permalink)
Xavier


 
 

New lines in regular expressions

Hello,

I am trying to search for a string pattern that includes new lines with the
select-string cmdlet. I was looking for something like \n to match a new line
in the regular expression but I can't seem to find anything.

Also, what would be the general syntax to include a new line in a string
(not speaking of regular expressions here) ?

Any help is appreciated.

Thanks,
Xavier


My System SpecsSystem Spec
Old 12-19-2007   #2 (permalink)
Shay Levi


 
 

Re: New lines in regular expressions

Hi

You can use the `n escape sequence:

$stringWithLineBreaks = "Power`nShell`nRocks!"
$stringWithLineBreaks

Power
Shell
Rocks!


-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic


Quote:

> Hello,
>
> I am trying to search for a string pattern that includes new lines
> with the select-string cmdlet. I was looking for something like \n to
> match a new line in the regular expression but I can't seem to find
> anything.
>
> Also, what would be the general syntax to include a new line in a
> string (not speaking of regular expressions here) ?
>
> Any help is appreciated.
>
> Thanks,
> Xavier

My System SpecsSystem Spec
Old 12-19-2007   #3 (permalink)
Shay Levi


 
 

Re: New lines in regular expressions


# create a here-string with line breaks
PS:23 >$hs = @"
power
shell
rocks!
"@


PS:24 >$hs
power
shell
rocks

# test for line break , same as $hs -match "`n"
PS:25 >$hs -match "\n"
True

PS:26 >$hs -match "power\n"
True

# replace
PS:27 >$new = $hs -replace "power\n","power"
PS:28 >$new
powershell
rocks!

# same applies with `n
PS:29 >$new = $hs -replace "power`n","power"
PS:30 >$new
powershell
rocks!

# find string with line break
PS:39 >Select-String -InputObject $new -Pattern "powershell\n" -Quiet
True


# same with `n
PS:40 >Select-String -InputObject $new -Pattern "powershell`n" -Quiet
True


-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic


Quote:

> Hello,
>
> I am trying to search for a string pattern that includes new lines
> with the select-string cmdlet. I was looking for something like \n to
> match a new line in the regular expression but I can't seem to find
> anything.
>
> Also, what would be the general syntax to include a new line in a
> string (not speaking of regular expressions here) ?
>
> Any help is appreciated.
>
> Thanks,
> Xavier

My System SpecsSystem Spec
Old 12-19-2007   #4 (permalink)
Xavier


 
 

Re: New lines in regular expressions

Thanks for the reply Shay,

I just tried `n and it does work in strings, but I still couldn't get the
select-string cmdlet to select a string pattern over multiple lines.
Actually, I don't know whether select-string is able to do that or not (it
might only apply the search pattern on each line) or if i'm using wrong
regular expressions.

Any idea on that ?

Thanks,
Xavier

"Shay Levi" wrote:
Quote:

> Hi
>
> You can use the `n escape sequence:
>
> $stringWithLineBreaks = "Power`nShell`nRocks!"
> $stringWithLineBreaks
>
> Power
> Shell
> Rocks!
>
>
> -----
> Shay Levi
> $cript Fanatic
> http://scriptolog.blogspot.com
> Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic
>
>
>
Quote:

> > Hello,
> >
> > I am trying to search for a string pattern that includes new lines
> > with the select-string cmdlet. I was looking for something like \n to
> > match a new line in the regular expression but I can't seem to find
> > anything.
> >
> > Also, what would be the general syntax to include a new line in a
> > string (not speaking of regular expressions here) ?
> >
> > Any help is appreciated.
> >
> > Thanks,
> > Xavier
>
>
>
My System SpecsSystem Spec
Old 12-19-2007   #5 (permalink)
Shay Levi


 
 

Re: New lines in regular expressions

Xavier,

I know that matching text spanned accross multiple lines with line breaks
can be tricky (if possible)
not only in PowerShell, but this works (so I guess it's possible):

PS:23 >$hs = @"
power
shell
rocks!
"@

PS:24 >$new = $hs -replace "power`nshell","once again, powershell"
PS:25 >$new
once again, powershell
rocks!

Can you post a sample text with the regex pattern?

-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic


Quote:

> Thanks for the reply Shay,
>
> I just tried `n and it does work in strings, but I still couldn't get
> the select-string cmdlet to select a string pattern over multiple
> lines. Actually, I don't know whether select-string is able to do that
> or not (it might only apply the search pattern on each line) or if i'm
> using wrong regular expressions.
>
> Any idea on that ?
>
> Thanks,
> Xavier
> "Shay Levi" wrote:
>
Quote:

>> Hi
>>
>> You can use the `n escape sequence:
>>
>> $stringWithLineBreaks = "Power`nShell`nRocks!"
>> $stringWithLineBreaks
>> Power
>> Shell
>> Rocks!
>> -----
>> Shay Levi
>> $cript Fanatic
>> http://scriptolog.blogspot.com
>> Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic
Quote:

>>> Hello,
>>>
>>> I am trying to search for a string pattern that includes new lines
>>> with the select-string cmdlet. I was looking for something like \n
>>> to match a new line in the regular expression but I can't seem to
>>> find anything.
>>>
>>> Also, what would be the general syntax to include a new line in a
>>> string (not speaking of regular expressions here) ?
>>>
>>> Any help is appreciated.
>>>
>>> Thanks,
>>> Xavier

My System SpecsSystem Spec
Old 12-20-2007   #6 (permalink)
Xavier


 
 

Re: New lines in regular expressions

Thank you both for your input.

I find it very confusing that this doesn't work :

PS C:\Users\Xavier\Desktop> $somevar = "power`nshell`nrocks"
PS C:\Users\Xavier\Desktop> select-string -inputobject $somevar -pattern
"power`nshell" -quiet
True
PS C:\Users\Xavier\Desktop> set-content test.txt $somevar
PS C:\Users\Xavier\Desktop> get-content test.txt
power
shell
rocks
PS C:\Users\Xavier\Desktop> get-content test.txt | select-string "power"
-quiet
True
PS C:\Users\Xavier\Desktop> get-content test.txt | select-string
"power\nshell" -quiet
PS C:\Users\Xavier\Desktop> get-content test.txt | select-string
"power`nshell" -quiet
PS C:\Users\Xavier\Desktop>

So the pattern matches the variable content, but not the file content.
From what I understand, this is because the first variable is a string, and
the second is an array.
Keith's solution will work in that case :

PS C:\Users\Xavier\Desktop> $lines = get-content test.txt
PS C:\Users\Xavier\Desktop> $text = [string]::Join("`n", $lines)
PS C:\Users\Xavier\Desktop> select-string -inputobject $text -pattern
"power`nshell" -quiet
True

Thanks a lot,
Xavier


"Shay Levi" wrote:
Quote:

>
> # create a here-string with line breaks
> PS:23 >$hs = @"
> power
> shell
> rocks!
> "@
>
>
> PS:24 >$hs
> power
> shell
> rocks
>
> # test for line break , same as $hs -match "`n"
> PS:25 >$hs -match "\n"
> True
>
> PS:26 >$hs -match "power\n"
> True
>
> # replace
> PS:27 >$new = $hs -replace "power\n","power"
> PS:28 >$new
> powershell
> rocks!
>
> # same applies with `n
> PS:29 >$new = $hs -replace "power`n","power"
> PS:30 >$new
> powershell
> rocks!
>
> # find string with line break
> PS:39 >Select-String -InputObject $new -Pattern "powershell\n" -Quiet
> True
>
>
> # same with `n
> PS:40 >Select-String -InputObject $new -Pattern "powershell`n" -Quiet
> True
>
>
> -----
> Shay Levi
> $cript Fanatic
> http://scriptolog.blogspot.com
> Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic
>
>
>
Quote:

> > Hello,
> >
> > I am trying to search for a string pattern that includes new lines
> > with the select-string cmdlet. I was looking for something like \n to
> > match a new line in the regular expression but I can't seem to find
> > anything.
> >
> > Also, what would be the general syntax to include a new line in a
> > string (not speaking of regular expressions here) ?
> >
> > Any help is appreciated.
> >
> > Thanks,
> > Xavier
>
>
>
My System SpecsSystem Spec
Old 12-23-2007   #7 (permalink)
Shay Levi


 
 

Re: New lines in regular expressions


You can check the "Windows PowerShell Cookbook" by Lee Holmes (O'reilly),
recipe #5.7:

"By default, regular expressions do not search across lines, but you can
use the singline (?s)
option to instruct them to do so."


PS > "Hello `n World" -match "Hello.*World"
False

PS > "Hello `n World" -match "(?s)Hello.*World"
True


Hope this helps

-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic


Quote:

> Thank you both for your input.
>
> I find it very confusing that this doesn't work :
>
> PS C:\Users\Xavier\Desktop> $somevar = "power`nshell`nrocks"
> PS C:\Users\Xavier\Desktop> select-string -inputobject $somevar
> -pattern
> "power`nshell" -quiet
> True
> PS C:\Users\Xavier\Desktop> set-content test.txt $somevar
> PS C:\Users\Xavier\Desktop> get-content test.txt
> power
> shell
> rocks
> PS C:\Users\Xavier\Desktop> get-content test.txt | select-string
> "power"
> -quiet
> True
> PS C:\Users\Xavier\Desktop> get-content test.txt | select-string
> "power\nshell" -quiet
> PS C:\Users\Xavier\Desktop> get-content test.txt | select-string
> "power`nshell" -quiet
> PS C:\Users\Xavier\Desktop>
> So the pattern matches the variable content, but not the file content.
> From what I understand, this is because the first variable is a
> string, and
> the second is an array.
> Keith's solution will work in that case :
> PS C:\Users\Xavier\Desktop> $lines = get-content test.txt
> PS C:\Users\Xavier\Desktop> $text = [string]::Join("`n", $lines)
> PS C:\Users\Xavier\Desktop> select-string -inputobject $text -pattern
> "power`nshell" -quiet
> True
> Thanks a lot,
> Xavier
> "Shay Levi" wrote:
>
Quote:

>> # create a here-string with line breaks
>> PS:23 >$hs = @"
>> power
>> shell
>> rocks!
>> "@
>> PS:24 >$hs
>> power
>> shell
>> rocks
>> # test for line break , same as $hs -match "`n"
>> PS:25 >$hs -match "\n"
>> True
>> PS:26 >$hs -match "power\n"
>> True
>> # replace
>> PS:27 >$new = $hs -replace "power\n","power"
>> PS:28 >$new
>> powershell
>> rocks!
>> # same applies with `n
>> PS:29 >$new = $hs -replace "power`n","power"
>> PS:30 >$new
>> powershell
>> rocks!
>> # find string with line break
>> PS:39 >Select-String -InputObject $new -Pattern "powershell\n" -Quiet
>> True
>> # same with `n
>> PS:40 >Select-String -InputObject $new -Pattern "powershell`n" -Quiet
>> True
>> -----
>> Shay Levi
>> $cript Fanatic
>> http://scriptolog.blogspot.com
>> Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic
Quote:

>>> Hello,
>>>
>>> I am trying to search for a string pattern that includes new lines
>>> with the select-string cmdlet. I was looking for something like \n
>>> to match a new line in the regular expression but I can't seem to
>>> find anything.
>>>
>>> Also, what would be the general syntax to include a new line in a
>>> string (not speaking of regular expressions here) ?
>>>
>>> Any help is appreciated.
>>>
>>> Thanks,
>>> Xavier

My System SpecsSystem Spec
Old 12-25-2007   #8 (permalink)
Xavier


 
 

Re: New lines in regular expressions

Thank you Shay, that's good to know.

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
regular expressions - why are they so hard to compose and decipher? PowerShell
$Variables into Regular expressions ?! help PowerShell
regular expressions to replace but keep character? VB Script
How to work with regular expressions PowerShell
Regular expressions PowerShell


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