0

Here is the powershell script

$configFiles = Get-ChildItem . *.scd -rec
foreach ($file in $configFiles)
{
    (Get-Content $file.PSPath) |
    Foreach-Object { 
    $_ -replace '<Terminal .+\/>', '' `
       -replace '<Terminal [.|\n]*?<\/Terminal>', '' `
       -replace '<Private type=\"ABB.*?<\/Private>', '' `
       -replace '<ConnectivityNode.*?>.*?<\/ConnectivityNode>', '' `
       -replace '\r\n[\t\n\v\f\r ]+\r\n', '\r\n' 
    } |
    Set-Content $file.PSPath
}

The first replace is working just fine because the tag is single line but the rest are not working, nothing happens as they have other tags inside them as well. I wanted to know what's the correct Regular expression.

This is the group of tags that i want to replace(2nd replace) with ' ' what regular expression would work?

<Terminal name="T_grounded" connectivityNode="AA1/J1/Q01/grounded" 
    substationName="AA1" voltageLevelName="J1" bayName="Q01"
        cNodeName="grounded">
            <Private type="ABB SLD">
                <esld:Line>
                    <esld:Via x="244" y="116" />
                </esld:Line>
            </Private>
</Terminal> 

In Notepad++ it's pretty simple as you can just mark . matches new line and replace all but i don't know how we will adapt it in powershell script.

Thanks in advance, please help me out.

Inno BoY
  • 5
  • 4
  • 5
    Golden rule of programming written on stone tablets somewhere: don't use [Regex on X/HTML](https://stackoverflow.com/a/1732454/1422451)! Show us your original and desired output there are many DOM methods even XSLT to do what you need. – Parfait Aug 01 '17 at 14:39
  • It's not up to me it's the task i have been given i'm going to scratch my face please help me out this time next time i will commit suicide before denying the golden rule lol. – Inno BoY Aug 01 '17 at 19:24
  • Basically the output i want is the group of tags removed, that's why i'm replacing the tags with empty string. – Inno BoY Aug 01 '17 at 19:30
  • 1
    I refer the honorable gentleman back to the very first comment made on this question. – Bill_Stewart Aug 01 '17 at 19:45
  • Again i can't use any other thing, this is the task now please anyone who can help? – Inno BoY Aug 02 '17 at 12:39
  • I actually worked out how to do it for multiple lines and those regex work fine with everything(Notepad++ now don't need to mark . matches new line to make it work, online reg testers) it works with everything except powershell when run in powershell nothing happens can someone tell me why? – Inno BoY Aug 03 '17 at 12:01

1 Answers1

0

Using the mode modifier, single line, you can treat your input as if it was all one line. This makes the . match newline characters and stuff too. Some something like this:

$test = @"
some junk
<Terminal name="T_grounded" connectivityNode="AA1/J1/Q01/grounded" 
    substationName="AA1" voltageLevelName="J1" bayName="Q01"
        cNodeName="grounded">
            <Private type="ABB SLD">
                <esld:Line>
                    <esld:Via x="244" y="116" />
                </esld:Line>
            </Private>
</Terminal> 
some more junk
"@

$test -replace "(?s)<Terminal.*Terminal>"," "

should rip out what you want.

mod modifier is added into the regex using (?s) and then you can just look for the start and end of the node.

edit: in this case you'll also need to read your input in as raw data instead of line by line.

Sambardo
  • 714
  • 2
  • 9
  • 15