-
Oct 24th, 2013, 07:25 AM
#1
Thread Starter
Junior Member
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 " < " ?? " >" ?
-
Oct 24th, 2013, 08:03 AM
#2
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
-
Oct 24th, 2013, 08:04 AM
#3
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)
-
Oct 24th, 2013, 08:05 AM
#4
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")
-
Oct 24th, 2013, 08:58 AM
#5
Thread Starter
Junior Member
Re: Find text between two characters
Originally Posted by Arnoutdv
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.
Originally Posted by Bonnie West
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:
Originally Posted by stum
[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...
-
Oct 24th, 2013, 09:01 AM
#6
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?
-
Oct 24th, 2013, 09:06 AM
#7
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
-
Oct 24th, 2013, 11:44 AM
#8
Thread Starter
Junior Member
Re: Find text between two characters
Originally Posted by Arnoutdv
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...
-
Oct 24th, 2013, 12:12 PM
#9
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)
-
Oct 24th, 2013, 12:41 PM
#10
Thread Starter
Junior Member
Re: Find text between two characters
Originally Posted by Arnoutdv
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|