我想制作一个简单的PowerShell脚本,该脚本:
\input{my_folder/my_file}
文件内容本身替换出现的我的第一个步骤是匹配不同的文件名以导入,虽然下面的代码不仅输出文件名也\include{file1}
,\include{file2}
等等。
$ms = Get-Content ms.tex -Raw
$environment = "input"
$inputs = $ms | Select-String "\\(?:input|include)\{([^}]+)\}" -AllMatches | Foreach {$_.matches}
Write-Host $inputs
我以为使用括号会创建一个匹配的组,但这会失败,您能否向我解释为什么以及仅获取文件名而不是完全匹配的正确方法是什么?
在regex101上,此regexp\\(?:input|include)\{([^}]+)\}
似乎可以正常工作。
您正在寻找正向后看和正向前看:
@'
Some line
\input{my_folder/my_file}
Other line
'@ | Select-String '(?<=\\input{)[^}]+(?=})' -AllMatches | Foreach {$_.matches}
结果
Groups : {0}
Success : True
Name : 0
Captures : {0}
Index : 18
Length : 17
Value : my_folder/my_file
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句