Announcement

Collapse
No announcement yet.

Regex help needed

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

    Regex help needed

    I need to extract the string between the "" after the "user" string in the following string

    { "Response":"ok","pin":"1234567","user":"goldriver" }

    I found looking on an online regex tester and debugger that (?:[^"]*"){11}\K[^"]* worked but I tried to port it in my scipt and I have errors reported:

    Dim regex As Regex = New Regex((?:[^"]*"){11}\K[^"]*)

    I guess the regex expression I found is not supported in VB script

    anyome can help with this ?

    thanks

    #2
    The regents parameter should b a string. Put a quote at the start and end inside the parentheses and then eac quote in between should be duplicated.

    Comment


      #3
      Michael McSharry thanks but even with the following I have errors:

      Dim regex As Regex = New Regex("(?:[^""]*""){11}\K[^""]*")

      Comment


        #4
        I do not use regex enough to use it without constant reference to the documentation. Based upon your post the regex characters work. Note that different environments support different regex syntax so what works in a java browser sandbox may not work in a .net script.

        More info than “ it does not work” will likely have a better chance of getting help. The usual debug approach is to simplify until something works and the expand step by step.

        Comment


          #5
          I believe the issue may be that you are using the word "regex" as a variable when it is already a reserved word. However, as Michael indicated, without the exact error messages and also without seeing the whole script, its hard to help. Try
          Code:
          Dim MyRegex As Regex = New Regex("(?:[^""]*""){11}\K[^""]*")
          HS 3.0.0.548: 1990 Devices 1172 Events
          Z-Wave 3.0.1.262: 126 Nodes on one Z-Net

          Comment


            #6
            Also, just checking that Regex sample in the tools I use, it does not appear to have a proper syntax. Not sure where you got it. Try this instead:

            Code:
            Dim ResultString As String
            Try
                ResultString = Regex.Match(myVariable, "user"":""(.*)""").Groups(1).Value
            Catch ex As ArgumentException
                'Syntax error in the regular expression
            End Try
            PS I'm not a Regex expert at all, I just use some tools that I play around with (including one that generates vb.net code) until I get something that seems to work reliably.
            HS 3.0.0.548: 1990 Devices 1172 Events
            Z-Wave 3.0.1.262: 126 Nodes on one Z-Net

            Comment


              #7
              Not an answer to the regex question but as an alternative you could also use the vb split function and use colon as the separator. I am assuming that the form would always be the same.

              Comment


                #8
                Originally posted by AllHailJ View Post
                Not an answer to the regex question but as an alternative you could also use the vb split function and use colon as the separator. I am assuming that the form would always be the same.
                Good point, and there are also functions to parse/deserialize the json, so that’s another option.
                HS 3.0.0.548: 1990 Devices 1172 Events
                Z-Wave 3.0.1.262: 126 Nodes on one Z-Net

                Comment


                  #9
                  sparkman here is the error returned in the log even with your version of the regex expression:
                  mars-24 06:30:07 Error Compiling script C:\Program Files (x86)\HomeSeer HS3\scripts\BLtest.vb: Une exception a été levée par la cible d'un appel.
                  AllHailJ Thanks for the hint

                  I am on the emergency team today and don't have a lot of time to dig further, I will dig deeper after this day, thanks a lot

                  Comment


                    #10
                    Originally posted by goldriver View Post
                    sparkman here is the error returned in the log even with your version of the regex expression:
                    If my translation skills are working ok, then that is a generic error potentially caused by other problems within the script. Without seeing the rest of the script, I can’t help further. If you don’t want to post it here, direct message me instead.
                    HS 3.0.0.548: 1990 Devices 1172 Events
                    Z-Wave 3.0.1.262: 126 Nodes on one Z-Net

                    Comment


                      #11
                      goldriver


                      Here's a complete example script that works correctly with no errors:

                      Code:
                      Imports System.Text.RegularExpressions
                      
                      Public Sub Main(ByVal Parms As String)
                      
                          Dim myVariable As String = "{ ""Response"":""ok"",""pin"":""1234567"",""user"":""goldriver"" }" 
                          Dim ResultString As String = ""
                          Try
                              ResultString = Regex.Match(myVariable, "user"":""(.*)""").Groups(1).Value
                              hs.writelog("Regex Example",ResultString)
                          Catch ex As ArgumentException
                              hs.writelog("Regex Example","Something is wrong in the regex")
                          End Try
                      
                      End Sub
                      HS 3.0.0.548: 1990 Devices 1172 Events
                      Z-Wave 3.0.1.262: 126 Nodes on one Z-Net

                      Comment


                        #12
                        sparkman you made my day ! Thank you so much, this works !

                        Comment

                        Working...
                        X