![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| | |||||||
| | Vista - Problem with [environment]::NewLine and Regular Expression |
| |
| 11-27-2007 | #1 |
| | Problem with [environment]::NewLine and Regular Expression Here is my powershell code: $tabFile = [string]::Join([environment]::NewLine, (Get-Content "C:\data\!!sandbox\dhcp\dhcp3tab")) ##$tabFile = @" ##[Subnet : "172.29.11.0(VLAN19)"] ## Subnet Address = 172.29.11.0 ## Subnet Mask = 255.255.255.0 ## Lease Time = 259200 ## Config Options = 00 00 00 00 03 04 AC 1D 0B 01 ## Zone Reference = bdokendalls_com_au.BDOK ## Domain Name = bdokendalls.com.au ## Subnet Type = 1 ## ##[Subnet Address Range : "Subnet Server.VLAN19.BDOK"] ## Start Address = 172.29.11.0 ## End Address = 172.29.11.0 ## DHCP Server Reference = "DHCP_NWC-BNE-B.BRIS.BDOK" ## Range Type = 99 ## ##[Subnet Address Range : "LAN.VLAN19.BDOK"] ## Start Address = 172.29.11.50 ## End Address = 172.29.11.250 ## DHCP Server Reference = "DHCP_NWC-BNE-B.BRIS.BDOK" ## Range Type = 3 ## DNS Update Option = 2 ##"@ $RegexOptions = [System.Text.RegularExpressions.RegexOptions] $options = ($RegexOptions::Multiline -bor $RegexOptions::IgnoreCase) $regex = @" ^\[(?<name>[^\r\n]+)\ :\ "(?<desc>[^"\r\n]*)"\](?:\n\t(?<lines>.+?))*$ "@ $reg = new-object System.Text.RegularExpressions.Regex($regex, $options) $matches = $reg.Matches($tabFile) $matches.Count $matches | Foreach-Object { $_.Groups["name"].Value $_.Groups["desc"].Value $_.Groups["lines"].Captures | foreach-Object { $_.Value } } I've tested this regular expression in a program called the regulator and it works perfectly. Unfortunately when running it in powershell it does not match anything. If I remove the $ it does match some of what i want but not all. Is this a bug? -- ********************** Jacob |
| My System Specs |
| 11-27-2007 | #2 |
| | Re: Problem with [environment]::NewLine and Regular Expression On Nov 28, 9:41 am, Jacob <jacob(AT)hfws.net.nospam> wrote: Quote: > Here is my powershell code: > > $tabFile = [string]::Join([environment]::NewLine, (Get-Content > "C:\data\!!sandbox\dhcp\dhcp3tab")) > > ##$tabFile = @" > ##[Subnet : "172.29.11.0(VLAN19)"] > ## Subnet Address = 172.29.11.0 > ## Subnet Mask = 255.255.255.0 > ## Lease Time = 259200 > ## Config Options = 00 00 00 00 03 04 AC 1D 0B 01 > ## Zone Reference = bdokendalls_com_au.BDOK > ## Domain Name = bdokendalls.com.au > ## Subnet Type = 1 > ## > ##[Subnet Address Range : "Subnet Server.VLAN19.BDOK"] > ## Start Address = 172.29.11.0 > ## End Address = 172.29.11.0 > ## DHCP Server Reference = "DHCP_NWC-BNE-B.BRIS.BDOK" > ## Range Type = 99 > ## > ##[Subnet Address Range : "LAN.VLAN19.BDOK"] > ## Start Address = 172.29.11.50 > ## End Address = 172.29.11.250 > ## DHCP Server Reference = "DHCP_NWC-BNE-B.BRIS.BDOK" > ## Range Type = 3 > ## DNS Update Option = 2 > ##"@ > > $RegexOptions = [System.Text.RegularExpressions.RegexOptions] > $options = ($RegexOptions::Multiline -bor $RegexOptions::IgnoreCase) > > $regex = @" > ^\[(?<name>[^\r\n]+)\ :\ "(?<desc>[^"\r\n]*)"\](?:\n\t(?<lines>.+?))*$ > > "@ > > $reg = new-object System.Text.RegularExpressions.Regex($regex, $options) > > $matches = $reg.Matches($tabFile) > $matches.Count > > $matches | Foreach-Object { > $_.Groups["name"].Value > $_.Groups["desc"].Value > $_.Groups["lines"].Captures | foreach-Object { > $_.Value > } > > } > > I've tested this regular expression in a program called the regulator and it > works perfectly. Unfortunately when running it in powershell it does not > match anything. If I remove the $ it does match some of what i want but not > all. Is this a bug? > -- > ********************** > Jacob The '$' at the end of your expression is not necessary, and you are missing a '\r' before your "lines" group: ^\[(?<name>[^\r\n]+)\ :\ "(?<desc>[^"\r\n]*)"\](?:\r\n\t(?<lines>. +?))* I hope this helps. Jeff |
| My System Specs |
| 11-28-2007 | #3 |
| | Re: Problem with [environment]::NewLine and Regular Expression Hi Jeff, Thanks for your reply. As it turns out the program I was using called Regulator was working when it shouldn't have been. The final regex was \[(?<name>[^\r\n]+)\ :\ "(?<desc>[^"\r\n]*)"\](?:\r\n\t(?<lines>.+?))*\r\n\r\n -- ********************** Jacob "Jeff" wrote: Quote: > On Nov 28, 9:41 am, Jacob <jacob(AT)hfws.net.nospam> wrote: Quote: > > Here is my powershell code: > > > > $tabFile = [string]::Join([environment]::NewLine, (Get-Content > > "C:\data\!!sandbox\dhcp\dhcp3tab")) > > > > ##$tabFile = @" > > ##[Subnet : "172.29.11.0(VLAN19)"] > > ## Subnet Address = 172.29.11.0 > > ## Subnet Mask = 255.255.255.0 > > ## Lease Time = 259200 > > ## Config Options = 00 00 00 00 03 04 AC 1D 0B 01 > > ## Zone Reference = bdokendalls_com_au.BDOK > > ## Domain Name = bdokendalls.com.au > > ## Subnet Type = 1 > > ## > > ##[Subnet Address Range : "Subnet Server.VLAN19.BDOK"] > > ## Start Address = 172.29.11.0 > > ## End Address = 172.29.11.0 > > ## DHCP Server Reference = "DHCP_NWC-BNE-B.BRIS.BDOK" > > ## Range Type = 99 > > ## > > ##[Subnet Address Range : "LAN.VLAN19.BDOK"] > > ## Start Address = 172.29.11.50 > > ## End Address = 172.29.11.250 > > ## DHCP Server Reference = "DHCP_NWC-BNE-B.BRIS.BDOK" > > ## Range Type = 3 > > ## DNS Update Option = 2 > > ##"@ > > > > $RegexOptions = [System.Text.RegularExpressions.RegexOptions] > > $options = ($RegexOptions::Multiline -bor $RegexOptions::IgnoreCase) > > > > $regex = @" > > ^\[(?<name>[^\r\n]+)\ :\ "(?<desc>[^"\r\n]*)"\](?:\n\t(?<lines>.+?))*$ > > > > "@ > > > > $reg = new-object System.Text.RegularExpressions.Regex($regex, $options) > > > > $matches = $reg.Matches($tabFile) > > $matches.Count > > > > $matches | Foreach-Object { > > $_.Groups["name"].Value > > $_.Groups["desc"].Value > > $_.Groups["lines"].Captures | foreach-Object { > > $_.Value > > } > > > > } > > > > I've tested this regular expression in a program called the regulator and it > > works perfectly. Unfortunately when running it in powershell it does not > > match anything. If I remove the $ it does match some of what i want but not > > all. Is this a bug? > > -- > > ********************** > > Jacob > Jacob, > > The '$' at the end of your expression is not necessary, and you are > missing a '\r' before your "lines" group: > > ^\[(?<name>[^\r\n]+)\ :\ "(?<desc>[^"\r\n]*)"\](?:\r\n\t(?<lines>. > +?))* > > I hope this helps. > > Jeff > |
| My System Specs |
| 11-28-2007 | #4 |
| | Re: Problem with [environment]::NewLine and Regular Expression "Jacob" <jacob(AT)hfws.net.nospam> wrote in message news:81896FDD-16F1-4FE4-BBF8-5A37A596367D@xxxxxx Quote: > Here is my powershell code: > > $tabFile = [string]::Join([environment]::NewLine, (Get-Content > "C:\data\!!sandbox\dhcp\dhcp3tab")) > > ##$tabFile = @" > ##[Subnet : "172.29.11.0(VLAN19)"] > ## Subnet Address = 172.29.11.0 > ## Subnet Mask = 255.255.255.0 > ## Lease Time = 259200 > ## Config Options = 00 00 00 00 03 04 AC 1D 0B 01 > ## Zone Reference = bdokendalls_com_au.BDOK > ## Domain Name = bdokendalls.com.au > ## Subnet Type = 1 > ## > ##[Subnet Address Range : "Subnet Server.VLAN19.BDOK"] > ## Start Address = 172.29.11.0 > ## End Address = 172.29.11.0 > ## DHCP Server Reference = "DHCP_NWC-BNE-B.BRIS.BDOK" > ## Range Type = 99 > ## > ##[Subnet Address Range : "LAN.VLAN19.BDOK"] > ## Start Address = 172.29.11.50 > ## End Address = 172.29.11.250 > ## DHCP Server Reference = "DHCP_NWC-BNE-B.BRIS.BDOK" > ## Range Type = 3 > ## DNS Update Option = 2 > ##"@ > > > $RegexOptions = [System.Text.RegularExpressions.RegexOptions] > $options = ($RegexOptions::Multiline -bor $RegexOptions::IgnoreCase) > > $regex = @" > ^\[(?<name>[^\r\n]+)\ :\ "(?<desc>[^"\r\n]*)"\](?:\n\t(?<lines>.+?))*$ > > "@ > > > $reg = new-object System.Text.RegularExpressions.Regex($regex, $options) > > > $matches = $reg.Matches($tabFile) > $matches.Count > > $matches | Foreach-Object { > $_.Groups["name"].Value > $_.Groups["desc"].Value > $_.Groups["lines"].Captures | foreach-Object { > $_.Value > } > > } > > I've tested this regular expression in a program called the regulator and > it > works perfectly. Unfortunately when running it in powershell it does not > match anything. If I remove the $ it does match some of what i want but > not > all. Is this a bug? the @" "@ here string) is just a `n char. If you were to change this: $tabFile = [string]::Join([environment]::NewLine, (Get-Content "C:\data\!!sandbox\dhcp\dhcp3tab")) to $tabFile = [string]::Join("`n", (Get-Content "C:\data\!!sandbox\dhcp\dhcp3tab")) You would get further. Or you could change the regex: 224> $regex = [regex]'(?im)^\[(?<name>[^:]+) : "(?<desc>[^"]*)"\]\r\n(?:\t(?<lines>.+)(\z|\r\n(?!^$)))*' 225> $regex.Matches($tabfile) | %{$OFS="`n ";"$($_.Groups['name'].Value) : $($_.Groups['desc'].Value) : $($_.Groups['lines'].Captures)"} Subnet : 172.29.11.0(VLAN19) : Subnet Address = 172.29.11.0 Subnet Mask = 255.255.255.0 Lease Time = 259200 Config Options = 00 00 00 00 03 04 AC 1D 0B 01 Zone Reference = bdokendalls_com_au.BDOK Domain Name = bdokendalls.com.au Subnet Type = 1 Subnet Address Range : Subnet Server.VLAN19.BDOK : Start Address = 172.29.11.0 End Address = 172.29.11.0 DHCP Server Reference = "DHCP_NWC-BNE-B.BRIS.BDOK" Range Type = 99 Subnet Address Range : LAN.VLAN19.BDOK : Start Address = 172.29.11.50 End Address = 172.29.11.250 DHCP Server Reference = "DHCP_NWC-BNE-B.BRIS.BDOK" Range Type = 3 DNS Update Option = 2 Rather than mess with the RegexOptions enum I would just use (?im) at the beginning of the regex. It's a lot less to type. :-) I also recommended using single quoted strings unless you need PowerShell variable substitution. Otherwise you have to worry about escaping certain characters that have special meaning to PowerShell like $. -- Keith |
| My System Specs |
| 11-28-2007 | #5 |
| | Re: Problem with [environment]::NewLine and Regular Expression Will do, thanks for the feedback! -- ********************** Jacob "Keith Hill [MVP]" wrote: Quote: > "Jacob" <jacob(AT)hfws.net.nospam> wrote in message > news:81896FDD-16F1-4FE4-BBF8-5A37A596367D@xxxxxx Quote: > > Here is my powershell code: > > > > $tabFile = [string]::Join([environment]::NewLine, (Get-Content > > "C:\data\!!sandbox\dhcp\dhcp3tab")) > > > > ##$tabFile = @" > > ##[Subnet : "172.29.11.0(VLAN19)"] > > ## Subnet Address = 172.29.11.0 > > ## Subnet Mask = 255.255.255.0 > > ## Lease Time = 259200 > > ## Config Options = 00 00 00 00 03 04 AC 1D 0B 01 > > ## Zone Reference = bdokendalls_com_au.BDOK > > ## Domain Name = bdokendalls.com.au > > ## Subnet Type = 1 > > ## > > ##[Subnet Address Range : "Subnet Server.VLAN19.BDOK"] > > ## Start Address = 172.29.11.0 > > ## End Address = 172.29.11.0 > > ## DHCP Server Reference = "DHCP_NWC-BNE-B.BRIS.BDOK" > > ## Range Type = 99 > > ## > > ##[Subnet Address Range : "LAN.VLAN19.BDOK"] > > ## Start Address = 172.29.11.50 > > ## End Address = 172.29.11.250 > > ## DHCP Server Reference = "DHCP_NWC-BNE-B.BRIS.BDOK" > > ## Range Type = 3 > > ## DNS Update Option = 2 > > ##"@ > > > > > > $RegexOptions = [System.Text.RegularExpressions.RegexOptions] > > $options = ($RegexOptions::Multiline -bor $RegexOptions::IgnoreCase) > > > > $regex = @" > > ^\[(?<name>[^\r\n]+)\ :\ "(?<desc>[^"\r\n]*)"\](?:\n\t(?<lines>.+?))*$ > > > > "@ > > > > > > $reg = new-object System.Text.RegularExpressions.Regex($regex, $options) > > > > > > $matches = $reg.Matches($tabFile) > > $matches.Count > > > > $matches | Foreach-Object { > > $_.Groups["name"].Value > > $_.Groups["desc"].Value > > $_.Groups["lines"].Captures | foreach-Object { > > $_.Value > > } > > > > } > > > > I've tested this regular expression in a program called the regulator and > > it > > works perfectly. Unfortunately when running it in powershell it does not > > match anything. If I remove the $ it does match some of what i want but > > not > > all. Is this a bug? > The newline that you have inserted in your regular expression string (via > the @" "@ here string) is just a `n char. If you were to change this: > > $tabFile = [string]::Join([environment]::NewLine, (Get-Content > "C:\data\!!sandbox\dhcp\dhcp3tab")) > > to > > $tabFile = [string]::Join("`n", (Get-Content > "C:\data\!!sandbox\dhcp\dhcp3tab")) > > You would get further. Or you could change the regex: > > 224> $regex = [regex]'(?im)^\[(?<name>[^:]+) : > "(?<desc>[^"]*)"\]\r\n(?:\t(?<lines>.+)(\z|\r\n(?!^$)))*' > 225> $regex.Matches($tabfile) | %{$OFS="`n ";"$($_.Groups['name'].Value) > : $($_.Groups['desc'].Value) : $($_.Groups['lines'].Captures)"} > Subnet : 172.29.11.0(VLAN19) : Subnet Address = 172.29.11.0 > Subnet Mask = 255.255.255.0 > Lease Time = 259200 > Config Options = 00 00 00 00 03 04 AC 1D 0B 01 > Zone Reference = bdokendalls_com_au.BDOK > Domain Name = bdokendalls.com.au > Subnet Type = 1 > Subnet Address Range : Subnet Server.VLAN19.BDOK : Start Address = > 172.29.11.0 > End Address = 172.29.11.0 > DHCP Server Reference = "DHCP_NWC-BNE-B.BRIS.BDOK" > Range Type = 99 > Subnet Address Range : LAN.VLAN19.BDOK : Start Address = 172.29.11.50 > End Address = 172.29.11.250 > DHCP Server Reference = "DHCP_NWC-BNE-B.BRIS.BDOK" > Range Type = 3 > DNS Update Option = 2 > > Rather than mess with the RegexOptions enum I would just use (?im) at the > beginning of the regex. It's a lot less to type. :-) I also recommended > using single quoted strings unless you need PowerShell variable > substitution. Otherwise you have to worry about escaping certain characters > that have special meaning to PowerShell like $. > > -- > Keith > |
| My System Specs |
![]() |
| Thread Tools | |
| |
| Similar Threads for: Problem with [environment]::NewLine and Regular Expression | ||||
| Thread | Forum | |||
| Regular Expression help C# | .NET General | |||
| Regular Expression for ../ | .NET General | |||
| Help with a regular expression | VB Script | |||
| regular expression help | VB Script | |||
| simple regular expression | PowerShell | |||