Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex MS VB For Application-读取txt文件中的href值_Regex_Vba - Fatal编程技术网

Regex MS VB For Application-读取txt文件中的href值

Regex MS VB For Application-读取txt文件中的href值,regex,vba,Regex,Vba,我的txt文件(links.txt)包含以下几个链接 <A HREF="file1.oft">File 1</A> <A HREF="file2.oft">File 2</A> <A HREF="file3.oft">File 3</A> <A HREF="file4.oft">File 4</A> <A HREF="file5.oft">File 5</A> 我需要的是

我的txt文件(links.txt)包含以下几个链接

<A HREF="file1.oft">File 1</A>
<A HREF="file2.oft">File 2</A>
<A HREF="file3.oft">File 3</A>
<A HREF="file4.oft">File 4</A>
<A HREF="file5.oft">File 5</A>

我需要的是打开文件并读取链接的值(文件1、文件2等)

我该怎么做?正则表达式?我想这很简单,但我能找到我需要完成的事情

??? strRegX = "<\s*A(.|\n)*?\s*>((.|\n)*?)<\s*\/A\s*>"  ?????

Const ForReading = 1
Set objTextFile = objFSO.OpenTextFile("links.txt", ForReading)
Do Until objTextFile.AtEndOfStream
***** CODE GOES HERE ******
Loop
???strRegX=“((.|\n)*?)”?????
常数ForReading=1
设置objTextFile=objFSO.OpenTextFile(“links.txt”,用于读取)
直到objTextFile.AtEndOfStream
*****代码在这里******
环

提前谢谢

我真的很讨厌正则表达式。我会做得更“老式”:

选项显式
私有子showLinks()
将内容设置为字符串
作为整数的Dim pos
'读取文件内容
content=readFile(“C:\Temp\links.txt”)
'搜索链接
安装时执行(内容“”)
内容=中间(内容,位置+1)
'查找结束标记的开始

pos=InStr(content,“类似于此表达式的内容应该为您提供可以在代码中轻松阅读的组:
\..*
谢谢。我不得不对其进行一些修改,但它可以很好地满足我的需要。非常感谢:-)
Option Explicit

Private Sub showLinks()
    Dim content As String
    Dim pos As Integer

    ' read file content
    content = readFile("C:\Temp\links.txt")

    ' search for links
    Do While InStr(content, "<A HREF")
        ' find begin of link
        pos = InStr(content, "<A HREF")
        content = Mid(content, pos + 7)

        ' find closing >
        pos = InStr(content, ">")
        content = Mid(content, pos + 1)

        ' find begin of closing tag
        pos = InStr(content, "<")

        ' print out link text
        Debug.Print Left(content, pos - 1)
    Loop
End Sub

Private Function readFile(ByVal pFile As String) As String
    Dim ret As String
    Dim row As String

    ' create file handle
    Dim hnd As Integer
    hnd = FreeFile

    ' open file
    Open pFile For Input As hnd

    ' read file
    Do Until EOF(hnd)
        Line Input #hnd, row
        ret = ret & row
    Loop

    ' close file
    Close hnd

    ' return content
    readFile = ret
End Function