Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find all strings within a string

I get StrTxt as html string by http request response text. I want to find all occurrences of '"string"' in the StrTxt.

Something like this.

for each string in StrTxt
StrTxt = "all matched strings from StrTxt"
do something StrTxt.

Edit This is tagged as possible duplicate but it's not. How to loop through each word in a word document - VBA Macro explains how to find string in document and not string.

It is just simple. How to find all strings withing strings? Isn't my title explains everything?

Edit 2

From answer of Ansgar Wiechers I tried following.

Do
i = InStr(strtxt, "startstring")
      If i > 0 Then
        strtxt = Mid(strtxt, i, Len(strtxt) - i)
        i = InStr(InStr(strtxt, "midstring") + 1, strtxt, "endstring")
        If i > 0 Then
         strtxt = Left(strtxt, i + Len(endstring)) ' I am using i+4 as I know the length
        WScript.Echo strtxt
        End If
      End If
Loop While i > 0

It gives only one occurences. How to loop correctly?

like image 966
Rahul Avatar asked May 05 '26 03:05

Rahul


1 Answers

If you want to use InStr to search a string for all occurrences of a particular substring you need to call the function repeatedly, starting each new search (at least) one character after the last match.

response = "..."  'your HTTP response string
srch     = "..."  'the string you want to find in the response

start = 1
Do
  pos = InStr(start, response, srch, vbTextCompare)
  If pos > 0 Then
    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 you want the comparison to be case-sensitive replace vbTextCompare with vbBinaryCompare.


Edit: For finding patterns that start with some string, contain another, and end with a third one it's probably best to use a regular expression. @TylerStandishMan already showed the basic principle in his answer, but there are some things to observe in your 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

For Each m In re.Execute(response)
  WScript.Echo m
Next

Some characters in a regular expression have a special meanings (e.g. . matches any character except newlines), so you need to make sure that any such character in your start, mid and end terms is properly escaped (e.g. use \. for matching a literal dot). In case the substring you want to match spans more than one line you need those parts of the expression that match arbitrary text between your search terms to include newline characters (e.g. [\s\S] to match any whitespace or non-whitespace character). You may also want to make the match non-greedy, otherwise you'd get a single match from the first occurrence of startTerm to the last occurrence of endTerm. That's what the modifier *? is for.

like image 73
Ansgar Wiechers Avatar answered May 08 '26 15:05

Ansgar Wiechers