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 " < " ?? " >" ?
Printable View
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 " < " ?? " >" ?
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
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")
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")
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.
But I'll not know wich tags has been used by the user...
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?
It's basically the same here: http://www.vbforums.com/showthread.p...ds-and-symbols
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:
Passing the example text from post #1 to that function yields the following items: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
EDITCode:"<Name>"
"<Enterprise_Name>"
"<birth_Message>"
"<Signature>"
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