关注正则表达式:
(?<=href(\s+)?=(\s+)?")(?!(\s+)?http)(?!//).+(?=")
按预期与测试文章一起使用:
href="//www.google-analytics.com/analytics.js">
href="https://www.google-analytics.com/analytics.js">
href="index.html">
href="..\index.html">
href="main.css">
href="..\assets\main.css">
href = " ..\assets\main.css ">
正如您在此处看到的那样:https://t.co/PC0U9br3vn
然而:
[$string] $string = Get-Content sample.txt
[$string] $regex = '(?<=href(\s+)?=(\s+)?")(?!(\s+)?http)(?!(\s+)?//)(?!(\s+)?mailto).+(?=")'
$newString = $string -replace $regex, "..\$&"
$string
$newString
产生以下输出:
//www.google-analytics.com/analytics.js"> href=" https://www.google-analytics.com/analytics.js"> href="index.html"> href="..\index.html"> href=" main.css"> href="..\assets\main.css"> href = " ..\assets\main.css "> href = "mailto://email@domain "> href = "..\..\..\assets\main.css"
//www.google-analytics.com/analytics.js"> href=" https://www.google-analytics.com/analytics.js"> href="..\index.html"> href="..\index.html"> href=" main.css"> href="..\assets\main.css"> href = " ..\assets\main.css "> href = "mailto://email@domain "> href = "..\..\..\assets\main.css"
因为只有第一篇文章正在进行中。
相同的脚本正在其他地方工作,其中替换字符串不使用正则表达式并且是一个简单的字符串。
答案 0 :(得分:2)
输入的类型错误:
[$string] $string = Get-Content sample.txt
然而,字符串数组起作用:
[$string[]] $string = Get-Content sample.txt