regex - Powershell: Read a section of a file into a variable -


i'm trying create kind of polyglot script. it's not true polyglot because requires multiple languages perform, although can "bootstrapped" either shell or batch. i've got part down no problem.

the part i'm having trouble bit of embedded powershell code, needs able load current file memory , extract section written in yet language, store in variable, , pass interpreter. have xml-like tagging system i'm using mark sections of file in way not conflict of other languages. markers this:

lang_a_code # <{langb}>    ... code in language b ...    ... code in language b ...    ... code in language b ... # <{/langb}> lang_c_code 

the #'s comment markers, comment markers can different things depending on language of section.

the problem have can't seem find way isolate section of file. can load entire file memory, can't stuff between tags out. here current code:

@echo off setlocal enabledelayedexpansion  powershell -executionpolicy unrestricted -command ^      $re = '(?m)^<{langb}^>(.*)^<{/langb}^>';^     $lang_b_code = ([io.file]::readalltext(^'%0^') -replace $re,'$1');^     echo "${re}";^     echo "contents: ${lang_b_code}"; 

everything i've tried far results in entire file being output in contents rather code between markers. i've tried different methods of escaping symbols used in markers, results in same thing.

note: use of ^ required because top-level interpreter batch, hangs on angle brackets , other random things.

since there 1 block, can use regex

$re = '(?s)^<{langb}^>(.*)^^.*^<{/langb}^>';^ 

but -match operator, , access text using $matches[1] variable set result of -match.

so, after regex declaration, use

[io.file]::readalltext(^'%0^') -match $re;^ echo $matches[1]; 

Comments

Popular posts from this blog

jquery - How do you format the date used in the popover widget title of FullCalendar? -

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -