vba - Find all strings within a string -
i strtxt html string http request response text. want find occurrences of '"string"' in strtxt.
something this.
for each string in strtxt strtxt = "all matched strings strtxt" strtxt.
edit tagged possible duplicate it's not. how loop through each word in word document - vba macro explains how find string in document , not string.
it simple. how find strings withing strings? isn't title explains everything?
edit 2
from answer of ansgar wiechers tried following.
do = instr(strtxt, "startstring") if > 0 strtxt = mid(strtxt, i, len(strtxt) - i) = instr(instr(strtxt, "midstring") + 1, strtxt, "endstring") if > 0 strtxt = left(strtxt, + len(endstring)) ' using i+4 know length wscript.echo strtxt end if end if loop while > 0
it gives 1 occurences. how loop correctly?
if want use instr
search string occurrences of particular substring need call function repeatedly, starting each new search (at least) 1 character after last match.
response = "..." 'your http response string srch = "..." 'the string want find in response start = 1 pos = instr(start, response, srch, vbtextcompare) if pos > 0 start = pos + 1 'alternatively: start = pos + len(srch) wscript.echo pos wscript.echo mid(response, pos, len(srch)) end if loop while pos > 0
if want comparison case-sensitive replace vbtextcompare
vbbinarycompare
.
edit: finding patterns start string, contain another, , end third 1 it's best use regular expression. @tylerstandishman showed basic principle in his answer, there things observe in scenario.
response = "..." 'your http response string startterm = "startstring" midterm = "midstring" endterm = "endstring" set re = new regexp re.pattern = startterm & "[\s\s]*?" & midterm & "[\s\s]*?" & endterm re.global = true re.ignorecase = true 'if required each m in re.execute(response) wscript.echo m next
some characters in regular expression have special meanings (e.g. .
matches character except newlines), need make sure such character in start, mid , end terms escaped (e.g. use \.
matching literal dot). in case substring want match spans more 1 line need parts of expression match arbitrary text between search terms include newline characters (e.g. [\s\s]
match whitespace or non-whitespace character). may want make match non-greedy, otherwise you'd single match first occurrence of startterm
last occurrence of endterm
. that's modifier *?
for.
Comments
Post a Comment