Posts: 1,031
Threads: 246
Joined: Jul 2022
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:
$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
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)
Posts: 1,336
Threads: 61
Joined: Jul 2006
10-28-2022, 12:42 PM
(This post was last modified: 10-28-2022, 12:51 PM by Kevin.)
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
Posts: 1,031
Threads: 246
Joined: Jul 2022
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
Posts: 1,336
Threads: 61
Joined: Jul 2006
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
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
Posts: 1,031
Threads: 246
Joined: Jul 2022
10-28-2022, 10:52 PM
(This post was last modified: 10-28-2022, 11:46 PM by Davider.)
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
Posts: 1,336
Threads: 61
Joined: Jul 2006
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
_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
Posts: 1,031
Threads: 246
Joined: Jul 2022
11-03-2022, 12:36 AM
(This post was last modified: 11-03-2022, 12:43 AM by Davider.)
Quote:_s.replacerx("#if.*(?s)(.*?)#endif" "")
-> This replacement method always leaves a blank line, It's the same in PowerShell
Quote:_s.replacerx("(???!\n)\s)*\n){2,}" "[]")
-> This may affect all blank lines
Posts: 1,336
Threads: 61
Joined: Jul 2006
11-03-2022, 04:19 AM
(This post was last modified: 11-03-2022, 04:20 AM by Kevin.)
use this instead
_s.replacerx("#if.*(?s)(.*?)#endif" "")
_s.replacerx("(?:(?:(?!\n)\s)*\n){2,}" "[][]")
out _s
]
Posts: 1,031
Threads: 246
Joined: Jul 2022
A better way
_s.replacerx("#if.*(?s)(.*?)#endif" "")
to
_s.replacerx("#if.*(?s)(.*?)#endif\r\n" "")