Announcement

Collapse
No announcement yet.

Searching a string for multiple instances of word?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Searching a string for multiple instances of word?

    Currently I have this:

    Code:
    mystring = "The quick brown <span>192.168.100.10</span> fox jumps over the lazy dogs"
    
    result1 = string.match(mystring, '[0-9]+.[0-9]+.[0-9]+.[0-9]') --find the ip address in a string
    result2 = String.Find(mystring, result1, 1, false); --get the start position of the IP
    
    target_table = {result1,result2}; --store the values in a table for later
    
    Dialog.Message("Notice", target_table[1] .. "\n\n" .."At position: " .. target_table[2], MB_OK, MB_ICONINFORMATION, MB_DEFBUTTON1); --show the restuls from the table
    This works ok - but if my string changes to this:

    Code:
    mystring = "The quick brown <span>192.168.100.10</span> fox jumps over the lazy dogs <span>192.168.100.20</span>"
    How would I be able to find both IP addresses and put them in the table? is that possible with APMS? if so how can it be done?

    Thanks.

  • #2
    Use a loop and modified strings. So:
    1) Search your string for the IP.
    2) Make a copy of your string and remove the IP you just found
    3) Rinse and repeat until no more IPs are found.

    Comment


    • #3
      Originally posted by HobbsyJr View Post
      Use a loop and modified strings. So:
      1) Search your string for the IP.
      2) Make a copy of your string and remove the IP you just found
      3) Rinse and repeat until no more IPs are found.
      Thanks, how would I do that though - do you have an example?

      The best I could come up with was this, but if the amount of IP's it finds changes the program crashes:

      Code:
      mystring = TextFile.ReadToString("AutoPlay\\Docs\\filewithIP.txt");
      
      find1 = string.match(mystring, "<span>" .. '[0-9]+.[0-9]+.[0-9]+.[0-9]+', "0") --find the ip address in a string
      strip1 = String.Replace(find1, "<span>", "", false);
      result1 = String.Find(mystring, find1, 1, false); --get the start position of the IP
      
      add1 = result1+1
      find2 = string.match(mystring, "<span>" .. '[0-9]+.[0-9]+.[0-9]+.[0-9]+', add1) --find the ip address in a string
      strip2 = String.Replace(find2, "<span>", "", false);
      result2 = String.Find(mystring, find2, add1, false);
      
      table = {strip1,strip2}; --store the values in a table for later
      Dialog.Message("Notice", table[1] .. "\n" .. table[2], MB_OK, MB_ICONINFORMATION, MB_DEFBUTTON1); --show the restuls from the table
      Thanks for looking.

      Comment


      • #4
        Use a loop that checks the condition on every iteration.
        E.G.
        Code:
        -- Copy the string into a variable first (lets call it varStr)
        
        while(  ) -- check VarStr for an IP
        do
           -- copy the IP to your table
           -- Remove the IP from VarStr
        end
        Read up on While Loops (and other kinds of Loop) to understand how they work.
        Remember:
        The statement inside the While parentheses should be conditional (==).
        Iterate any table positions within the loop otherwise you'll keep overwriting the same table position.

        Good Luck.

        Comment


        • #5
          Originally posted by HobbsyJr View Post
          Use a loop that checks the condition on every iteration.
          E.G.
          Code:
          -- Copy the string into a variable first (lets call it varStr)
          
          while(  ) -- check VarStr for an IP
          do
             -- copy the IP to your table
             -- Remove the IP from VarStr
          end
          Read up on While Loops (and other kinds of Loop) to understand how they work.
          Remember:
          The statement inside the While parentheses should be conditional (==).
          Iterate any table positions within the loop otherwise you'll keep overwriting the same table position.

          Good Luck.
          OK thanks, I'll have a look at that.

          Comment

          Working...
          X