I have been grappling with this problem all day, hopefully someone can help me with a code snippet or at least a pointer in the right direction.
I am trying to extract data from a string where the data required appears between two strings multiple times inside the source string,.
For example I would like to extract all occurrences of text from a string that appears between
where the number of occurrences is not predictable and the position of each occurrence is not predictable.
Any help would be appreciated.
here is a start
dim v() as string
this will return an array of every string segment that began with “
you will have to clean up the rest
That sounds like a reason to use Regular Expressions. I’m no RegEx expert, but Kem (who I call an expert) did a great webinar on RegEx a couple weeks ago:
Using Regular Expressions
dim s as string = "For example I would like to extract all occurrences of text from a string that appears between <h3> and </h3> where the number of occurrences is not predictable and the position of each occurrence is not predictable."
dim v() as string
dim fifi() as string
dim occurrences() as string
for i as integer = 0 to v.Ubound
if instr(v(i),"</h3>")>0 then
fifi = split(v(i),"</h3>")
This will extract all occurrences and place them in the array occurrences(). You got the number of found occurrences as occurrences.ubound.
This short snippet ends with a msgbox that displays the first occurrence in your text :“and”.
Now wait for the wizard to appear : Kem as usual will come up with some wondrous cryptic magic
Thank you everyone for your assistance.
dim rx as new RegEx
rx.SearchPattern = "(?Umsi)<h3>(.*)(?=</h3>)</h3>"
dm arr() as string
dim match as RegExMatch = rx.Search( sourceText )
while match <> nil
arr.Append match.SubExpressionString( 1 )
match = rx.Search
The array arr will contain all the matches.