Results 1 to 3 of 3

Thread: How do I separate out text between two search terms in a string?

  1. #1
    Join Date
    Jul 2001
    Location
    Risør - Norway
    Posts
    5,477

    Default How do I separate out text between two search terms in a string?

    I need to get out a part of a response from the JRMC web server interface. It comes in this form (only the necessary excerpt):

    Code:
    <Response Status="OK"><Item Name="NumberZones">12</Item><Item Name="CurrentZoneID">10007</Item><Item Name="CurrentZoneIndex">8</Item><Item Name="ZoneName0">Tor</Item><Item Name="ZoneID0">0</Item><Item Name="ZoneGUID0">{3ECD3C04-2C29-4F23-8F99-0E134C0BE5D2}</Item><Item Name="ZoneName1">Stua</Item><Item Name="ZoneID1">10000</Item>
    What I need to do in this response is to search for ZoneID1 and then make the part 10000 into a variable. I can find the necessary bit easily enough (the variable sent into this is the number of the zone I need the ID from):

    Code:
    local Sone = 1
    local soneliste = socket.http.request("http://192.168.0.15/MCWS/v1/Playback/Zones")
    local i = string.find(soneliste, 'ZoneID'..Sone)
    print(i)
    local sonenummer = string.sub(soneliste, i+9, i+13)
    print(sonenummer)
    This gives me this output:

    Code:
    10000
    But if I try the first zone, zone 0, I get:
    Code:
    0</It
    Since that's only one digit I get 4 extra symbols. I could of course to an if then to check if the zone number is 0, but I have another place I would like to use the same stuff, and that's more varying what will be in the result. So is there a way to find first the search term and then the first instance of a second search term? So I can search for SoneID1 and then search for the first instance of < after that, and then use the part that's between them?
    Tor - managing director of the Cinema Inferno home theater and multi-zone sound system with Girder running the show in the back, NetRemote as the GUI and Media Center 17, PowerDVD and ZoomPlayer as playback software
    Hobsyssel mastiffs: http://www.hobsyssel.no

  2. #2
    Join Date
    May 2004
    Location
    Dallas, TX
    Posts
    1,125

    Default

    Not the most elegant solution, but it works.

    Code:
    local _, _, sonenummer0 = string.find(soneliste, 'ZoneID0">(%d+)<')
    local _, _, sonenummer1 = string.find(soneliste, 'ZoneID1">(%d+)<')
    print (sonenummer0,sonenummer1)
    Rick

    Girder 6.0 - Elk M1 Gold - JRMC 11.1 w/ M-Audio Delta 410 - NetRemote - BlueIris DVR
    Girder 6 Plugins - Elk M1/EZ8, Pushover Connector and more on the way.


    Visit My Showcase

  3. #3
    Join Date
    Jul 2001
    Location
    Risør - Norway
    Posts
    5,477

    Default

    Thanks, Rick! It's more elegant than you think. Because I can do it with any zone like this:

    Code:
    local Sone = 11
    local soneliste = socket.http.request("http://192.168.0.15/MCWS/v1/Playback/Zones")
    local _, _, sonenummer = string.find(soneliste, 'ZoneID'..Sone..'">(%d+)<')
    print (sonenummer)
    So this gives the sonenummer (or actual ID) of 100023. And so on
    Tor - managing director of the Cinema Inferno home theater and multi-zone sound system with Girder running the show in the back, NetRemote as the GUI and Media Center 17, PowerDVD and ZoomPlayer as playback software
    Hobsyssel mastiffs: http://www.hobsyssel.no

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •