Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Gets the content between two tags
#1
Hi,

I want to get all the content between the two tags (#if ... #endif)

I have the following powershell code, requests the QM code with the same effect

Thanks in advance for any advice and help
david

Powershell code:
Code:
Copy      Help
$s = @'
public static void Main(string[] args)
{
#if A
    part1
#endif

part
part

#if V
    part2
#endif

part
part

#if C
    part3
#endif
}
'@ -split [Environment]::NewLine

$between = $false
$ss= Switch -regex ($s)
{
    '#if.+|#endif' {
        $between = !$between
    }
    Default {
        If ($between)
        {
            $_
        }
    }
}
$ss


Macro Macro10
Code:
Copy      Help
out
_s=
;public static void Main(string[] args)
;{
;#if A
;,part1
;#endif
;
;part
;part
;
;#if V
;,part2
;#endif
;
;part
;part
;
;#if C
;,part3
;#endif
;}

;Todo: get all the content between the two tags (#if ... #endif)
#2
Code:
Copy      Help
str s1 a; int t
foreach s1  _s
,if(s1.begi("#if")) t=1; continue
,if(s1.begi("#endif")) t=0; continue
,if(t=1) a.addline(s1 1)        
out a
#3
Thank you for your help

1._s.begi  It seems that regular expressions are not supported,
if #if or #endif is preceded by a space or tab  ?

2.How to keep these two tags and output them together, e.g: out:
#if A
    part1
#endif
#if V
    part2
#endif
#if C
    part3
#endif
#4
this will do both either output 
whole match
#if A
    part1
#endif
#if V
    part2
#endif
#if C
    part3
#endif 

or what's in between #if and #endif
part1
part2
part3
 
Code:
Copy      Help
ARRAY(str) a2
findrx(_s "#if.*(?s)(.*?)#endif" 0 4 a2)
for int'i 0 a2.len
,out a2[0 i];; output whole match
,;out a2[1 i].trim;;output just whats in between #if A(this part)#endif
#5
Thanks, works Well,

The following logic seems easier to understand, 
Is it possible to create a member function(_s.Match) like -match?

---------------------------------------------------
$between = $false
$s |
foreach{
        if ($_ -match '#if.+|#endif')
        {
                $between = !$between
        }
        else
        {
                if ($between -eq $true) { $_ }
        }
}

1.Deleting them(#if...#endif) using the following regular expression leaves an extra blank line

2.What does (?s) mean?
-------------------------------------------------
_s.replacerx("#if.*(?s).*?#endif" "")

3. When there are multiple lines of content, trim has no effect
out a2[1 i].trim
-------------------------------------------------
 #if A
     part1
     part1

 #endif
 
 part
 part
 
 #if V
     part2
     part2
     part2

 #endif
 
 part
 part
 
 #if C
     part3
     part3

 #endif
#6
while this is not a member function could be made into a function
shown here as a sub function for simplicity.
also shows how to fix #1
 
Code:
Copy      Help
_s=
;public static void Main(string[] args)
;{
;;#if A
;;;;;part1
;;;;;part1
;#endif
;
;part
;part
;
;#if V
;;;;;part2
;;;;;part2
;;;;;part2
;#endif
;
;part
;part
;
;#if C
;;;;;part3
;;;;;part3
;#endif
;}

out
;;;keep these two tags and output them together
str a1
sub.Match(_s a1 1)
out a1
out "-------------------------------------------------"
;;output only whats inbetween
str a2
sub.Match(_s a2 0)
out a2
;; delete (#if...#endif)
out "-------------------------------------------------"
_s.replacerx("#if.*(?s)(.*?)#endif" "")
_s.replacerx("(?:(?:(?!\n)\s)*\n){2,}" "[]")
out _s



#sub Match
function ~s ~&s2 mode
;;mode 1=output #if #endif everything in between. 0=output only whats inbetween

str s1;int t1
if mode&1
,foreach s1  s
,,if(matchw(s1 "*#if*")) t1=1;
,,if(t1=1) s2.addline(s1 1)
,,if(matchw(s1 "*#endif*")) t1=0;
else
,foreach s1  s
,,if(matchw(s1 "*#if*")) t1=1; continue
,,if(matchw(s1 "*#endif*")) t1=0; continue
,,if(t1=1) s2.addline(s1.ltrim 1)
ret
#7
Quote:_s.replacerx("#if.*(?s)(.*?)#endif" "")      
->      This replacement method always leaves a blank line, It's the same in PowerShell
 
Quote:_s.replacerx("(?Sad?Sad?!\n)\s)*\n){2,}" "[]") 
    ->      This may affect all blank lines
#8
use this instead 
Code:
Copy      Help
_s.replacerx("#if.*(?s)(.*?)#endif" "")
_s.replacerx("(?:(?:(?!\n)\s)*\n){2,}" "[][]")
out _s
]
#9
A better way

_s.replacerx("#if.*(?s)(.*?)#endif" "")
to
_s.replacerx("#if.*(?s)(.*?)#endif\r\n" "")


Forum Jump:


Users browsing this thread: 12 Guest(s)