I need a regular expression that matches the paragraph: '&Start a'(first in sample text) until '&end a' (last end from sample text). The problem is that sometimes '&end a' is not explicitly specified, and is sometimes written as '&end'. The problem is even bigger when you have '&Start b' and '&end b' (which is sometimes '&end' as well, hence the confusion).
A sample block of target for this regex is (sorry for putting it as code block):
junk text
&Start a <
fulfilling text
fulfilling text
&Start b
&Start c
&end c
fulfilling text
&end
&end <
junk text
So the regex should match all paragraph starting and ending with the lines which contain the < symbol, though it's not included in the original text. (i.e. with the &Start X we want, and skipping the '&Start Y' '&end' (or '&end Y') groups until the '&end' (or '&end X') we want.
This is not a simple implementation. The expression I am working with is the following:
&start a([^&]*)(&end a|&end)
Which maches well isolated '&start a' '&end' paragraphs, but when other '&start Y' lines come in between, the script gets confused. I might use some If statment that jumps the undesired blocks... Here is a more complicated approach of the case:
junk text
&Start a <
fulfilling text
fulfilling text
&Start b
&Start c
&end
fulfilling text
&end
&end <
junk text
Without specifying any '&end'. Note1: '&start X' is always defined, but '&end X' can be as well '&end', but always corresponds to the closest start upfront. Note2: I can't change much the structure of my regex because of stack overflow errors, but rather adapt it to this specific case.
Sorry for the weird explaination but I hope somebody can find any viable advise.
Thank you
Edit:
#@ -split "`n" | ForEach-Object { $_.trim() } |
$files = Get-ChildItem "$PSScriptRoot" # root path
for($i=0; $i -lt $files.Count; $i++){
#iterate through files from the current folder.
$data = Get-Content -Path $files[$i].FullName
# parse DisabledFeatures.txt file as array of strings (1 string per line of the file)
$feature = Get-Content DisabledFeatures.txt
#iterate for each string entry in $feature array (read from txt file)
for($counter=0; counter -lt $feature.Count; counter++){
#retrieve array value to use it in the main algorythm
$groupID = "$feature"
$data | ForEach-Object -Begin { $ignore = $false; $levels = 0 } -Process {
#Start ignoring text after we've found the trigger
if($_ -match "^`#ifdef $groupID") { $ignore = $true }
#Track nested groups
elseif($ignore) {
if ($_ -match '^`#ifdef') { $levels++ }
elseif ($_ -match '`#endif') {
if($levels -ge 1) { $levels-- }
#If no nesting, we've hit the end of our targeted group. Stop ignoring
else { $ignore = $false }
}
}
#Write line
else { $_ }
}
}
}