Excelmacros代码中的正则expression式不起作用

我有以下代码,并且由于读取对象'IRegExp2失败“的”方法'执行'“的错误消息,

Dim wrksht As Worksheet Dim pg As Page Dim regEx As New VBScript_RegExp_55.RegExp For Each wrksht In Worksheets For Each pg In wrksht.PageSetup.pages With regEx .IgnoreCase = True .Pattern = "[(ftp|FTP|http|HTTP|s|S)]{3,5}*[\:/]{3}*[a-zA-Z0-9\s]{2,}[\.]*[.A-Za-z0-9\s]{2,}" End With If regEx.Execute(pg.CenterFooter.Text) Then pg.CenterFooter.Text = regEx.Replace(pg.CenterFooter.Text, "") End If Next pg Next wrksht 

我正在寻找replaceURL( www.sampleurl.com /subfolder/testfile.pdf)的开始部分,并只保留尾部(www.sampleurl.com /subfolder/testfile.pdf ,减去.com之后的空格部分)

稍微比replace.com(每个评论)更通用…希望这会让你得到你需要的地方:

 Dim LR As Long LR = Cells(ActiveSheet.Rows.Count, 1).End(xlUp).Row For i = 1 To LR Cells(i, 1) = Replace(Cells(i, 1), "https://", "", , 1) Cells(i, 1) = Replace(Cells(i, 1), "http://", "", , 1) Cells(i, 1) = Replace(Cells(i, 1), "/", "(error)", , 1) Next i Range(Cells(1, 1), Cells(LR, 1)).Replace What:="*(error)", Replacement:="" 

我的假设是有一个列中的地址在其中。

我从https://stackoverflow.com/a/6041965/5947891复制正则expression式,以下是为我工作的最终版本。 基本上,这将扫描工作簿中的所有工作表,检查url模式,截断URL基( http://www.sample.com /subfolder/subfile.png),并保留尾部(例如http ://www.sample.com**/subfolder/subfile.png**)。 我在http后面添加空格来使href无效。

 Sub FixFooter() Dim oSection As Word.Section Dim oRange As Word.Range Dim var Dim wrksht As Worksheet Dim pg As Page Dim regEx As New RegExp 'New VBScript_RegExp_55.RegExp Dim strPattern As String strPattern1 = "(http|ftp|https){0,1}(\://){0,1}([\w_-]+(?:(?:\.[\w_-]+)+))/" For Each wrksht In Worksheets Set pg = wrksht.PageSetup.pages(1) With regEx .IgnoreCase = True .Global = True .MultiLine = True .Pattern = strPattern1 End With If regEx.Test(pg.CenterFooter.Text) Then pg.CenterFooter.Text = "/" & regEx.Replace(pg.CenterFooter.Text, "") End If Next wrksht End Sub