Announcement

Collapse
No announcement yet.

am I missing something

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

    am I missing something

    Hi Guys,

    Hoping someone might be able to help me with a very basic code I have been having a play with below. I have been using the REGEX (?<=\))(.*?)(?=\[) which i verified online using RegExr and it works fine, and when i use the string below, on the online tester it does extract the appropriate section (in Bold) however in my code it does not appear to do so. Basically i am trying to use this code to extract any text between ) and [. Can anyone see what i am doing wrong in my code for it to never match?

    Thanks!

    Code:
    ("Main","ALERT YGON2 F111111111 G&SC3 SLIPON WGROS1 REQUIRED TANKER WGROT1 REQUIRED 123 SAMPLE RD SUBURB /Road 1 RD //Road 2 RD SVSE 0000 F3 (156728) [B]NITHT1 WARAT1 WGROS1 WGROT1 YGONS1[/B] [WGRO]
    ")

    PHP Code:
    Imports System.Text.RegularExpressions

    Sub Main
    (parm as object)

    Try

    Dim InputStr as String parm.ToString
    if InputStr Nothing then Exit Sub
    Dim regex 
    As Regex = New Regex("(?<=\))(.*?)(?=\[)"'Has brigades

    Dim match As Match = regex.Match(InputStr)
    if Match.Success = True
     hs.writelog("Appliance Lookup",match.value)
    end if

    Catch ex As Exception
        hs.writelog("Appliance Lookup",ex.Message)
    End Try
    End Sub 
    HS3 PRO, Win10, WeatherXML, HSTouch, Pushover, UltraGCIR, Heaps of Jon00 Plugins, Just sold and about to move so very slim system.

    Facebook | Twitter | Flickr | Google+ | Website | YouTube

    #2
    Hi,

    I can really help you with regex. Using regex can be very powerfull but also very, very time consuming to master. Most developers hate to use it and so do i.

    But here's how i do this common task when doing html scrapping. I use a function i made for it so it's easy to reuse.
    One side note, i did have the function at hand here so i wrote this down from the top of my mind. Bare with me if there are errors in this piece of code.

    Code:
    ' FirstStr and SecondStr can be a single character or
    ' multiple characters to search for in InputStr
    'The result send back is what is found between the end
    'of FirstStr and the start of SecondStr
    Function FindAString(ByVal InputStr As String, ByVal FirstStr As String, ByVal SecondStr As String)
      Dim start As Integer = 0
      Dim result As String = String.empty
     
      'If there are less then 3 chars then there is nothing to grab.
      If Len(InputStr) < 3 Then return result
     
      start = Instr(InputStr, FirstStr )
      'Only do something if there is a FirstStr in the string
      If start > 0 Then
        'Grab the right part of the string
        InputStr = Right(InputStr, len(InputStr) - start + Len(FirstStr))
      
        'Now check for the SecondStr sign
        start = Instr(InputStr, SecondStr - 1 )
      
        If start > 0 Then
          'The result now will contain the requested piece
          result = Left(InputStr, start)
        End If
      End If
      return result
    End Function
    - Bram

    Send from my Commodore VIC-20

    Ashai_Rey____________________________________________________________ ________________
    HS3 Pro 3.0.0.534
    PIugins: ZMC audio | ZMC VR | ZMC IR | ZMC NDS | RFXcom | AZ scripts | Jon00 Scripts | BLBackup | FritzBox | Z-Wave | mcsMQTT | AK Ikea

    Comment


      #3
      Originally posted by travisdh View Post
      Hi Guys,

      Hoping someone might be able to help me with a very basic code I have been having a play with below. I have been using the REGEX (?<=\))(.*?)(?=\[) which i verified online using RegExr and it works fine, and when i use the string below, on the online tester it does extract the appropriate section (in Bold) however in my code it does not appear to do so. Basically i am trying to use this code to extract any text between ) and [. Can anyone see what i am doing wrong in my code for it to never match?

      Thanks!

      Code:
      ("Main","ALERT YGON2 F111111111 G&SC3 SLIPON WGROS1 REQUIRED TANKER WGROT1 REQUIRED 123 SAMPLE RD SUBURB /Road 1 RD //Road 2 RD SVSE 0000 F3 (156728) [B]NITHT1 WARAT1 WGROS1 WGROT1 YGONS1[/B] [WGRO]
      ")

      PHP Code:
      Imports System.Text.RegularExpressions

      Sub Main
      (parm as object)

      Try

      Dim InputStr as String parm.ToString
      if InputStr Nothing then Exit Sub
      Dim regex 
      As Regex = New Regex("(?<=\))(.*?)(?=\[)"'Has brigades

      Dim match As Match = regex.Match(InputStr)
      if Match.Success = True
       hs.writelog("Appliance Lookup",match.value)
      end if

      Catch ex As Exception
          hs.writelog("Appliance Lookup",ex.Message)
      End Try
      End Sub 
      I've not tried your code, but could this be something do with the fact that your regex variable is named the same as the Regex class?
      Nicolai L

      Comment


        #4
        NicolaiL got a good point here.

        In vb there is no such thing as case sentitive meaning like C# where start is something different then Start

        So these two would give a problem
        Dim regex As Regex
        Dim match As Match
        - Bram

        Send from my Commodore VIC-20

        Ashai_Rey____________________________________________________________ ________________
        HS3 Pro 3.0.0.534
        PIugins: ZMC audio | ZMC VR | ZMC IR | ZMC NDS | RFXcom | AZ scripts | Jon00 Scripts | BLBackup | FritzBox | Z-Wave | mcsMQTT | AK Ikea

        Comment


          #5
          The regex I am using I have used (albeit with a different regex pattern) but it did work when I was doing something like extracting a letter and number combination e.g. F12311323 it worked fine, just this regex seems to be causing issues.

          I am not tied to regex for the extraction component, however using the find in string example when my start string is ") " and my end string is " [" i get the following error:

          Conversion from string " [" to type 'Double' is not valid. This is confusing as I could not see the conversion from string to double?

          I have the imports for strings set up, do i need any other imports?

          Thanks
          HS3 PRO, Win10, WeatherXML, HSTouch, Pushover, UltraGCIR, Heaps of Jon00 Plugins, Just sold and about to move so very slim system.

          Facebook | Twitter | Flickr | Google+ | Website | YouTube

          Comment


            #6
            Sorry my fault.
            As said there could be typos in the code.
            The problem was that in tried to substract 1 from a string and that won't work.

            I re-did the code and succesfully tested it this time. Save it as a .vbs file and run it by double clicking it or from the cmd prompt with wscript [filename.vb]<FILENAME.VBS>

            Please note that when you use the .vb extention (VB.NET) you have to use a different syntax for the function, return value and declaration of the variables

            If you need the VB.NET version of this then i think you are capable enough to change this otherwise let me now if you need any help.

            Code:
            Option Explicit
              Dim testStr
              Dim result
              testStr = "ALERT YGON2 F111111111 G&SC3 SLIPON WGROS1 REQUIRED TANKER WGROT1 REQUIRED 123 SAMPLE RD SUBURB /Road 1 RD //Road 2 RD SVSE 0000 F3 (156728) NITHT1 WARAT1 WGROS1 WGROT1 YGONS1 [WGRO]"
              result = FindInString(testStr, ") ", " [" )
              wscript.echo result
            '======================================================
            ' FirstStr and SecondStr can be a single character or
            ' multiple characters to search for in InputStr
            ' The result send back is what is found between the end
            ' of FirstStr and the start of SecondStr
            ' That is the first occurance of firstStr and the the first occurance
            ' of secondStr found after firstStr.
            Function FindInString(ByVal InputStr, ByVal FirstStr, ByVal SecondStr)
              Dim start
              Dim ending
             
              'If there are less then 3 chars then there is nothing to grab.
              If Len(InputStr) < 3 Then return result
             
              start = Instr(InputStr, FirstStr )
              'Only do something if there is a FirstStr in the string
              If start > 0 Then
                'skip the FirstStr characters
                start = start  + Len(FirstStr)
                'Grab the right part of the string
                InputStr = Right(InputStr, len(InputStr) - start + Len(FirstStr))
             
                'Now check for the SecondStr sign
                ending = Instr(InputStr, SecondStr ) - 1
             
                If ending > 0 Then
                  'The result now will contain the requested piece
                  FindInString = Left(InputStr, ending)
                End If
              End If
            End Function
            - Bram

            Send from my Commodore VIC-20

            Ashai_Rey____________________________________________________________ ________________
            HS3 Pro 3.0.0.534
            PIugins: ZMC audio | ZMC VR | ZMC IR | ZMC NDS | RFXcom | AZ scripts | Jon00 Scripts | BLBackup | FritzBox | Z-Wave | mcsMQTT | AK Ikea

            Comment


              #7
              Thanks for your help, I will try and have a play with it this week and start to convert to vb.net and see how it goes .

              Thanks!
              HS3 PRO, Win10, WeatherXML, HSTouch, Pushover, UltraGCIR, Heaps of Jon00 Plugins, Just sold and about to move so very slim system.

              Facebook | Twitter | Flickr | Google+ | Website | YouTube

              Comment


                #8
                In the end this did the trick, which is really the same sort of thing you pointed me to. The only thing i need to do is add more error handing, in some strings there is no [ character at the end, so I will likely just do an Instr check for the [ character, if it exists call the function otherwise if not I will probably just use a string.right method where the length is the position at which the ")" occurs - total length of the string that way it gets around issues.

                For clarity, and hopefully so it helps others below is what i ended up using, the function is called byGetStringBetween(String,WheretoStart,WheretoFinish) which in my case was GetStringBetween(String,")","{")

                Hope that helps someone, thanks for the help other people gave me!

                PHP Code:
                Public Function GetStringBetween(ByVal InputText As String,ByVal starttext As String,ByVal endtext As String)
                Try
                    
                Dim startPos As Integer
                    Dim endPos 
                As Integer
                    Dim lenStart 
                As Integer
                    startPos 
                InputText.IndexOf(startTextStringComparison.CurrentCultureIgnoreCase)
                    if 
                startPos >= 0 Then
                        lenStart 
                startPos starttext.Length
                        endPos 
                InputText.IndexOf(endtextlenstartStringComparison.CurrentCultureIgnoreCase)
                        If 
                endPos >= 0 Then
                            
                return InputText.Substring(lenStartendPos lenStart)
                        
                End If
                    
                End If
                    return 
                "ERROR"
                Catch ex As Exception
                    hs
                .writelog("Get String Between Function *ERROR*",ex.Message)
                End Try
                End Function 
                HS3 PRO, Win10, WeatherXML, HSTouch, Pushover, UltraGCIR, Heaps of Jon00 Plugins, Just sold and about to move so very slim system.

                Facebook | Twitter | Flickr | Google+ | Website | YouTube

                Comment


                  #9
                  Just a thanks for everyone's help again.

                  I would have never been able to achieve any of this without the support offered by the various members of the community.

                  Thanks!
                  HS3 PRO, Win10, WeatherXML, HSTouch, Pushover, UltraGCIR, Heaps of Jon00 Plugins, Just sold and about to move so very slim system.

                  Facebook | Twitter | Flickr | Google+ | Website | YouTube

                  Comment

                  Working...
                  X