Results 1 to 4 of 4

Thread: [RESOLVED] help with Regular Expressions!

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2009
    Posts
    40

    Resolved [RESOLVED] help with Regular Expressions!

    Hey guys,

    Quick query...

    I'm trying to scan a long string and return any values that occur between the tags <FileName> and </FileName>

    I've got a bit of code here to use regular expressions to return the position of the occurunces of both substrings..

    Just wanted to find out if there is any way to scan for both substrings within one loop (as im currently doing 2, one for the first substring and one for the 2nd) so I can use something along these lines:
    Code:
     ringlist = Mid(XMLRESP, (startloc + 1), (endloc - 1))
                MsgBox(ringlist)
    to msg box the value between the substrings...

    either that or another approach to return the value between the 2 substrings (it can be of variable length)

    Code:
                Dim patternstart As String = "<FileName>"
                Dim patternend As String = "</FileName>"
                Dim matchesstart As MatchCollection = Regex.Matches(XMLRESP, patternstart)
                Dim matchesend As MatchCollection = Regex.Matches(XMLRESP, patternend)
                Dim startloc As String
                Dim endloc As String
                startloc = ""
                endloc = ""
                'The matches collection now contains Indexes and other information about all of the matches        
                For Each m As Match In matchesstart
                    startloc = startloc + m.Index.ToString
                Next
    
                For Each n As Match In matchesend
                    endloc = endloc + n.Index.ToString
                Next

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: help with Regular Expressions!

    vb Code:
    1. Dim rx As New Regex("(?<=<FileName>).+?(?=</FileName>)")
    2.  
    3. For Each m As Match In rx.Matches(yourString)
    4.     MsgBox(m.Value)
    5. Next

  3. #3

    Thread Starter
    Member
    Join Date
    Mar 2009
    Posts
    40

    Re: help with Regular Expressions!

    You truly are the king.

    Thanks dude,

    you've saved me a hell of a lot of hassle!

    dan

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: [RESOLVED] help with Regular Expressions!

    click the scales icon to the left of this message
    <--------------------------------------------

Tags for this Thread

Posting Permissions

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



Click Here to Expand Forum to Full Width