I have a text file that looks like this:
;MPV_F4(isHold, taps, state)
MPV_F5(isHold, taps, state)
{
if (taps == 1)
{
if (isHold == 0)
{
;[HV] T1 | cycle mute | This is long LONG
Sendinput, {U 0398} ;Θ ; ;[rr] [cycle mute ]
}
else
{
if (state)
{
while GetKeyState("f5", "p"){
Sendinput, {U 0399} ;Ι ; ;[rr] [add volume 10 ]
sleep, 55
}
}
; else
; {
; }
}
}
}
MPV_F6(isHold, taps, state)
;MPV_F7(isHold, taps, state)
;MPV_F8(isHold, taps, state)
;MPV_F9(isHold, taps, state)
;MPV_F11(isHold, taps, state)
MPV_N2(isHold, taps, state)
{
if (taps == 1)
{
if (isHold == 0)
{
;[HV] T1 | cycle mute | This is long LONG
Sendinput, {U 0398} ;Θ ; ;[rr] [cycle mute ]
}
else
{
if (state)
{
while GetKeyState("f5", "p"){
Sendinput, {U 0399} ;Ι ; ;[rr] [add volume 10 ]
sleep, 55
}
}
; else
; {
; }
}
}
}
;MPV_N3(isHold, taps, state)
;MPV_N4(isHold, taps, state)
;MPV_N5(isHold, taps, state)
which essentially consists of a repeating pattern of:
FunctionName(isHold, taps, state)
{
<Function Body>
}
I am trying to break it down into arrays that consists of the function name and body. I cant use the string MPV in my regex as there are other similar texts files whose function names does not contain MPV.
My exepected output is:
$MyVar [0] :
MPV_F5(isHold, taps, state)
{
if (taps == 1)
{
if (isHold == 0)
{
;[HV] T1 | cycle mute | This is long LONG
Sendinput, {U 0398} ;Θ ; ;[rr] [cycle mute ]
}
else
{
if (state)
{
while GetKeyState("f5", "p"){
Sendinput, {U 0399} ;Ι ; ;[rr] [add volume 10 ]
sleep, 55
}
}
; else
; {
; }
}
}
}
$MyVar [1] :
MPV_N2(isHold, taps, state)
{
if (taps == 1)
{
if (isHold == 0)
{
;[HV] T1 | cycle mute | This is long LONG
Sendinput, {U 0398} ;Θ ; ;[rr] [cycle mute ]
}
else
{
if (state)
{
while GetKeyState("f5", "p"){
Sendinput, {U 0399} ;Ι ; ;[rr] [add volume 10 ]
sleep, 55
}
}
; else
; {
; }
}
}
}
At first I tried to use Get-Content's -Delimeter
parameter:
$mytextfile = "c:\temp\mytextfile.txt"
Get-content $mytextfile -Delimiter '.*[^;]\(isHold, taps, state\)'
It keeps returning the entire content as a single object, After a few more variations I resorted to just using Get-Content -Raw
and then -Split
operator:
$MyVar = $mytextfile -Split (?m).*[^;]\(isHold, taps, state\)(?s).*\}
I continue to get unexpected results, most commonly the entire content being returned instead of arrays.
I am using RegEx101*, and options are closesly aligned to what powershell expects. I have tried many variations with no desired results.
Here is a link my RegEx101 page.
What could I be doing wrong here?
Any help would be truelly wellcome.
CodePudding user response:
Try the following:
$MyVar =
[regex]::Matches(
(Get-Content -Raw $mytextfile),
'(?sm)\w \(isHold, taps, state\)\s*\{(?:.(?!\w \(\w)) \}'
).Value
Note that this approach is computation-intensive and relies on detecting the end of a function not by properly detecting nested {
/ }
pairs, but by assuming that the presence of any subsequent <funcname>(<wordcharacter>
substring (e.g. 'MPV_F6(i'
) implies that the most recent }
ended the function body.