dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: String parsing help

  1. #1
    Join Date
    Feb 2019
    Posts
    1

    Red face String parsing help

    Hello,

    I am working on a little project that can download and trim web page source code.
    I am struggling to use the string parsing commands to get all text to the right of a specific line in the HTML code.

    The program so far is able to retrieve the source code to any given page, but needs to be able to find a specific string(s) and store all of the text to the right of said string. eg;

    Code:
    <meta property="og:title" content="example text" />
        <meta property="og:test" content="text to parse here" />
        <meta property="og:description" content="text"
    I am looking to only retrieve the text following
    Code:
    <meta property="og:test" content="
    and trim off the last
    Code:
     " />"
    All other lines to be ignored

    The size of this string can vary so a character count isn't reliable.

    Any help would be greatly appreciated

    Thanks!

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    12,986

    Re: String parsing help

    Why not use the MSXML to read the nodes. It offers methods to read elements, attributes and values. This approach would be simpler than trying to extract strings from an xml snippet.

  3. #3
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,319

    Re: String parsing help

    You could use Instr() and Mid$() to parse out the string you want. There are better ways but of you just need the one string this method would be fairly easy to do.

    first use Instr() to find the search string
    Next use Instr() to fin the end tag, in this case you would need to specify the start position for Instr() using the value gotten by the first Instr() above.
    Once you have these to values then you can use Mid$() to pull the string the is between them.
    For example
    Code:
    Dim TheString As String, StartPos As Integer, EndPos As Integer, SearchString As String, ResultString As String
    TheString = "<meta property=""og:title"" content=""example text"" />"
    TheString = TheString & "<meta property=""og:test"" content=""text to parse here"" />"
    TheString = TheString & "<meta property=""og:description"" content=""text"" />"
    
    SearchString = "<meta property=""og:test"""
    StartPos = InStr(TheString, SearchString)
    If StartPos Then
        StartPos = StartPos + Len(SearchString)
        EndPos = InStr(StartPos, TheString, "/>")
        If EndPos Then
            ResultString = Trim$(Mid$(TheString, StartPos, EndPos - StartPos))
        Else
            ResultString = "End Position not found"
        End If
    Else
        ResultString = "Search text not found"
    End If
    MsgBox ResultString
    Always use [code][/code] tags when posting code.

  4. #4
    Join Date
    Mar 2019
    Posts
    1

    Re: String parsing help

    thanks for this code.

  5. #5
    Join Date
    Nov 2014
    Posts
    3

    Re: String parsing help

    Yes, thanks a lot! It help me out too!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)