2 Replies Latest reply on Aug 29, 2019 8:00 AM by Tony Mottram

    regex not bringing back any result

    Tony Mottram
      Share This:

      Hi Everyone,

       

      Hoping someone can help.

       

       

      (this if is an extract from within a for loop)

              db_si      := search (in host traverse Host:HostedSoftware:RunningSoftware:SoftwareInstance

                                              where type = 'Oracle Database Server'

                                              and instance matches '^YY');                               

                 

               for i in db_si do

                      port    :=  regex.extract (i.listen_tcp_sockets[0], regex '(\d{4}$)');

                                  log.info ('port is %port%');

                      db_host := regex.extract (i.listen_tcp_sockets[0], regex '(^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})');

                                  log.info ('database host is %db_host%');

      ////////////////////////////////////////////////////////////////////////the regex for the port and db_host works as expected////////////////////////////////////////////////////

                   if i then       

                      env := i.instance;

                           log.info ('environment is %env%');  

                           if env = regex.extract (i.instance[0], regex '(?i)\w{4}prd\d')

                               then instance := 'Production';

                           elif env = 'XXXXBAU1'

                               then instance := 'Test';               

                           elif env = 'XXXXJIT1'

                               then instance := 'Test';

                           end if;

                          log.info ('%env% is %instance%');

       

      the actual regex is valid (checked using regex101)

       

      we have numerous databases across prod, test, dev (eg prd1, prd2, bau1, uat2, dev3), if i use the actual database name (as shown with the 2 test above) it shows as expected. tried substituting the actual name with a regex, (tried each of the below).

       

                           if env = regex.extract (i.instance[0], regex '(?i)\w{4}prd\d')

                               then instance := 'Production';

                           if env = regex '(?i)\w{4}prd\d'

                               then instance := 'Production';

                           if env =  '(?i)\w{4}prd\d'

                               then instance := 'Production';

       

      the log result (were regex used) i get using any of the above is always "XXXXPRD1 is      "

       

      I can go back to using the actual db names but doesn't seem efficient or best way to do it. Can someone please advise what i need to do to get this to work

       

      Regards,

      Tony

        • 1. Re: regex not bringing back any result
          Andrew Waters

          Your initial extraction of port is rather suspect. ports do not have to have 4 digits.

           

          You are not passing the correct thing into regex.extract. l is a SoftwareInstance. It has an attribute instance which is the instance name. For some reason you have chosen to pass only the first character of the instance name into regex.extract so it will never match. I assume what you want is

          if regex.extract(env, regex '(?i)\w{4}prd\d') then

            instance := 'Production';

          elif regex.extract(env, regex '(?i)\w{4}bau\d') then

            instance := 'Test';

          elif regex.extract(env, regex '(?i)\w{4}jit\d') then

            instance := 'Test';

          end if;

          Note that this is a search so you may want to start the search from the beginning of the string using ^

          • 2. Re: regex not bringing back any result
            Tony Mottram

            thanks Andrew

             

            i was misreading/understanding the regex.extract, thank you for showing the correct use. your assumption was correct by the way and now work as i intended.

             

            in relation to the port, we dont use the default port setting for oracle db servers, however, the default port setting anyway is 4 digits (1521)

             

            thanks for you quick response

             

            Regards,

            Tony