Results 1 to 10 of 10

Thread: Find text between two characters

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2013
    Posts
    30

    Talking Find text between two characters

    If I have a text like this:

    HI Mr. <Name>, We from <Enterprise_Name> are glad to celebrate with you your birthday !!
    <birth_Message>

    <Signature>


    Is there a way to find the text between the " < " ?? " >" ?

  2. #2
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,910

    Re: Find text between two characters

    Do you know the InStr method?
    It will return the location of a substring in a string.

    Untested code!
    Code:
      Dim lPos1 As Long
      Dim lPos2 As Long
      Dim sText As String, sSubString As String
    
      sText = "HI Mr. <Name>, We from <Enterprise_Name> are glad to celebrate with you your birthday !!"
    
      lPos1 = InStr(1, sText, "<")
      Do While lPos1 > 0
        lPos2 = InStr(lPos1 + 1, sText, ">")
        If lPos2 > 0 Then
          sSubString = Mid$(sText, lPos1 + 1, lPos2 - lPos1 - 1)
          Debug.Print sSubString
        Else
          Exit Do
        End If
        lPos1 = InStr(lPos2 + 1, sText, "<")
      Loop

  3. #3
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: Find text between two characters

    Whenever you want to find a substring within a string, you use either the InStr or InStrRev functions. Both functions returns the character position where the substring was located.

    So, what are you going to do once you've determined the position of those characters? Replace the text between them with something else? The Replace function can help you do that:

    Code:
    sText = Replace(sText, "<Name>", "Ghaleon")
    Last edited by Bonnie West; Oct 24th, 2013 at 08:17 AM. Reason: Added code
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  4. #4
    Hyperactive Member
    Join Date
    Oct 2013
    Posts
    389

    Re: Find text between two characters

    Code:
    Public Function GetBetween(ByRef sSearch As String, ByRef sStart As String, ByRef sStop As String, _
                                                        Optional ByRef lSearch As Long = 1) As String
        lSearch = InStr(lSearch, sSearch, sStart)
        If lSearch > 0 Then
            lSearch = lSearch + Len(sStart)
            Dim lTemp As Long
            lTemp = InStr(lSearch, sSearch, sStop)
            If lTemp > lSearch Then
                GetBetween = Mid$(sSearch, lSearch, lTemp - lSearch)
            End If
        End If
    End Function
    
    'Usage:Text1.Text =  Getbetween("123 get this 321","123","321")

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Sep 2013
    Posts
    30

    Re: Find text between two characters

    Quote Originally Posted by Arnoutdv View Post
    Do you know the InStr method?
    It will return the location of a substring in a string.
    Yes i do, but I don't know wich tag will be used, I said <Name" just as an example... There are like 20+ tags, and I will not know wich one the user will use.

    Quote Originally Posted by Bonnie West View Post
    Whenever you want to find a substring within a string, you use either the InStr or InStrRev functions. Both functions returns the character position where the substring was located.

    So, what are you going to do once you've determined the position of those characters? Replace the text between them with something else? The Replace function can help you do that:


    Quote Originally Posted by stum View Post
    [code]Public Function GetBetween(ByRef sSearch As String, ByRef sStart As String, ByRef sStop As String, _
    But I'll not know wich tags has been used by the user...

  6. #6
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,910

    Re: Find text between two characters

    The code provided by me and by stum will show the texts between the tags from the input string.

    What is what you want to do with the given text?

  7. #7
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,444

    Re: Find text between two characters

    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Sep 2013
    Posts
    30

    Re: Find text between two characters

    Quote Originally Posted by Arnoutdv View Post
    The code provided by me and by stum will show the texts between the tags from the input string.

    What is what you want to do with the given text?

    I'll replace the entire tag <Name> by a value using a recordset. But to use the recordeset.Fields(""), I need to know wich tag is...

  9. #9
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: Find text between two characters

    The following function searches the specified text for substrings that begin with a < and ends with a >. The characters between those delimiters can be one or more of either an alphanumeric character or an underscore. This function returns a Collection object containing the substrings it found:

    Code:
    Public Function FindTags(ByRef Text As String) As Collection
        Dim oMatch As Object
    
        Set FindTags = New Collection
    
        With CreateObject("VBScript.RegExp")
            .Global = True
            .Pattern = "<\w+>"
    
            For Each oMatch In .Execute(Text)
                FindTags.Add oMatch.Value
            Next
        End With
    End Function
    Passing the example text from post #1 to that function yields the following items:

    Code:
    "<Name>"
    "<Enterprise_Name>"
    "<birth_Message>"
    "<Signature>"
    EDIT

    You can use the following syntax to enumerate each found tag and make substitutions:

    Code:
    Dim i As Long, sText As String
    
    With FindTags(sText)
        For i = 1& To .Count
            sText = Replace(sText, .Item(i), Recordset.Fields(""))
        Next
    End With
    Last edited by Bonnie West; Oct 24th, 2013 at 12:23 PM.
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Sep 2013
    Posts
    30

    Re: Find text between two characters

    Quote Originally Posted by Arnoutdv View Post
    Do you know the InStr method?
    It will return the location of a substring in a string.

    Untested code!
    Code:
      Dim lPos1 As Long
      Dim lPos2 As Long
      Dim sText As String, sSubString As String
    
      sText = "HI Mr. <Name>, We from <Enterprise_Name> are glad to celebrate with you your birthday !!"
    
      lPos1 = InStr(1, sText, "<")
      Do While lPos1 > 0
        lPos2 = InStr(lPos1 + 1, sText, ">")
        If lPos2 > 0 Then
          sSubString = Mid$(sText, lPos1 + 1, lPos2 - lPos1 - 1)
          Debug.Print sSubString
        Else
          Exit Do
        End If
        lPos1 = InStr(lPos2 + 1, sText, "<")
      Loop
    Worked Perfect ! But one detail I forgot to mention, I'll need to replace this in a word.Doc =X I'll try to use this

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