我有一个powershell脚本,可以删除包含“.html”“。css”的行等等
但我需要的是能够删除整个文件名
使用模式....整个模式返回示例
......... \。html返回
SRC = “blank.html”
我的答案来自VB(有大量的工作和更多的研究)我想与你分享所有的结果,它不是很漂亮,但它的工作原理。有更简单的方法吗?
我已经对代码进行了评论,以帮助理解。
Private Sub find()
Dim reader As StreamReader = My.Computer.FileSystem.OpenTextFileReader(openWork.FileName)
Dim a As String
Dim SearchForThis As String
Dim allfilenames As New System.Text.StringBuilder
Dim first1 As String
Dim FirstCharacter As Integer
'Dim lines As Integer
SearchForThis = txtFind.Text
Do
a = reader.ReadLine 'reader.Readling
If a = "" Then
a = reader.ReadLine
End If
If a Is Nothing Then 'without this check the for loops run with bad data, but I can't check "a" without reading it first.
Else
For FirstCharacter = 2 To a.Length - SearchForThis.Length ' start at 2 to prevent errors in the ")" check
If Mid(a, FirstCharacter, SearchForThis.Length) = SearchForThis Then ' compare the line character by character to find the searchstring
If Mid(a, FirstCharacter - 1, 1) <> ")" Then ' checks for ")" just before the searchstring (a common problem with my .CSS finds)
For y = FirstCharacter To 1 Step -1
If Mid(a, y, 1) = Mid(a, FirstCharacter + SearchForThis.Length, 1) Then ' compares the character after searchstring till I find another one
Dim temp = Mid(a, y + 1, (FirstCharacter + SearchForThis.Length) - 1 - y) ' puts the entire filename into variable "temp"
allfilenames.Append(temp & Chr(13)) 'adds the contents of temp (and a carrage return) to the allfilenames stringbuilder
y = 1
Else
End If
Next
End If
End If
Next
End If
Loop Until a Is Nothing
Document.Text = allfilenames.ToString
reader.Close()
End Sub
(更新评论...感谢输入)
.css搜索文件中的每一行都是这样的。
addPbrDlg.html:12:<link rel=stylesheet href="swl_styles-5.0o-97581885.css" TYPE="text/css">
addPbrDlg.html:727: html(getFrame(statusFrame).strErrorMessage).css('color','red');
为此我要返回
swl_styles-5.0o-97581885.css
但不返回
statusFrame).strErrorMessage)的CSS
基本上我想从HTML代码中删除文件名 但如果我使用像
这样的模式............................. \。CSS
它将返回类似
的内容t href =“swl_styles-5.0o-97581885.css
最后......有些变量我不需要担心(由于我的个人情况),就像我知道所有网页都是“.html”所有图片都是“.gif”有“。 css“和”.js“文件以及我想拉的文件。但因为设计师非常一致,我知道没有任何惊喜文件(.jpg或.htm) 我还可以假设,如果文件名后面有一个引号,那么之前会有一个引号。与双引号相同。
感谢您迄今为止的投入......感谢您的时间和知识。
答案 0 :(得分:0)
您需要使用Regex
并执行此类操作
Dim files = Regex.Matches("<your whole file text>", "Your regex pattern");
你的regex
模式看起来像这样“\ Asrc =”“。+((\。html)|(。。css))”“)”。这可能是错误的,但当你直接跟随
Dim fileList as new List(of String)
For Each file as Match in files
' strip " src=" " and last " " "
fileList.Add(file.Value.Substring(5, file.Value.Length - 6))
Next