Ad Widget

Collapse

Multiple values for regular expression preprocessing

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • markfree
    Senior Member
    • Apr 2019
    • 868

    #1

    Multiple values for regular expression preprocessing

    Hey guys.

    I'm trying to filter some data from a text item.
    The item collects something like this:
    users[0].Name=default
    (...)
    users[1].Name=admin
    (...)
    I wish to get just the user's name, so, I tried to preprocess it with this regular expression:
    Code:
    Pattern = Name=(\w+)  Output = \1
    And it works, but just for the first occurrence. Other occurrences are ignored.

    Then I tried to add some flags
    Code:
    /Name=(\w+)/gm
    But that gives me an error
    cannot perform regular expression "/Name=(\w+)/gm" match for value of type "string": pattern does not match
    I'm not sure how to get multiple values that match that expression.
    Any ideas?
    Thanks
  • Semiadmin
    Senior Member
    • Oct 2014
    • 1625

    #2
    1. CSV to JSON, delimeter =, without header row
    2. JSONPath: $..['2']

    Comment

    • markfree
      Senior Member
      • Apr 2019
      • 868

      #3
      That works and I can capture values after "=" delimiter.
      But, see... My dataset comes with more then just username data. So I have something like this:
      Code:
      users[0].Id=3
      users[0].Name=default
      users[0].Group=user
      (...)
      users[1].Id=338
      users[1].Name=admin
      users[1].Group=admin
      (...)
      By using "JSONPath" step, it gives me a list with other data that are not interesting to me right now.
      Code:
      ["3","default","user",(...),"341","admin","admin",( ...)]
      I'm only looking for "Name" strings.

      I also tried trimming left string like "users[0]." but it only trims the first line.

      Comment

      • Noobz
        Senior Member
        • Jun 2020
        • 105

        #4
        Are you looking to get a single users name as a returned value or an array of all users names contained within the string?
        It looks like JSONPATH (Just add ".Name" to the above) could be used here as mentioned above, or use javascript preprocessing to do this with regex, I've had to make many items this way.

        Comment

        • markfree
          Senior Member
          • Apr 2019
          • 868

          #5
          By converting CSV to JSON with no header, column names are generated automatically. The dataset is converted like this.
          Code:
          [
          {
              "1":"users[0].Id",
              "2":"3"
          },
          {
              "1":"users[0].Name",
              "2":"default"
          },
          (...)
          {
              "1":"users[1].Id",
              "2":"339"
          },
          {
              "1":"users[1].Name",
              "2":"admin"
          },
          (...)
          ]


          So, "Name" data actually becomes a value for column 1.

          I'm not a big fan of Javascript but, if there's no other option, that might get the job done.

          Comment

          • Noobz
            Senior Member
            • Jun 2020
            • 105

            #6
            If you have an actual data set, I can take a look for you. I've just gone through a bunch of this myself. JS is always my last resort, but I've had to use it several times.

            Comment

            • markfree
              Senior Member
              • Apr 2019
              • 868

              #7
              Originally posted by Noobz
              If you have an actual data set, I can take a look for you. I've just gone through a bunch of this myself. JS is always my last resort, but I've had to use it several times.
              It is basically like this

              users[0].Id=3
              users[0].Name=default
              users[0].Group=user
              users[0].ClientType=GUI
              users[0].ClientAddress=Local
              users[0].LoginTime=28/08/2021 03:01:00
              users[1].Id=338
              users[1].Name=admin
              users[1].Group=admin
              users[1].ClientType=CGI
              users[1].ClientAddress=xxx.xxx.xxx.xxx
              users[1].LoginTime=03/09/2021 17:36:56
              There might be more users (users[n])

              Comment

              • Noobz
                Senior Member
                • Jun 2020
                • 105

                #8
                And what is the result you want? An array of all users? A particular user? An item prototype creating an item for each user?

                Comment

                • markfree
                  Senior Member
                  • Apr 2019
                  • 868

                  #9
                  Originally posted by Noobz
                  And what is the result you want? An array of all users? A particular user? An item prototype creating an item for each user?
                  This is not really a necessity of mine, but a request.
                  I'm chatting with users to better understand their needs.

                  I'll collect Name, Group, ClientType, ClientAddress and LoginTime.
                  At first, I thought of presenting these values with Data Overview widget. That's not definitive, though.
                  These data will probrably go to Grafana.

                  Comment

                  • Semiadmin
                    Senior Member
                    • Oct 2014
                    • 1625

                    #10
                    Originally posted by markfree
                    I'm only looking for "Name" strings.
                    Step 2:
                    JSONPath: $.[?(@.['1'] =~ "Name")].['2']

                    Comment

                    • markfree
                      Senior Member
                      • Apr 2019
                      • 868

                      #11
                      Originally posted by Semiadmin

                      Step 2:
                      JSONPath: $.[?(@.['1'] =~ "Name")].['2']
                      This works.
                      Now, I can collect multiple "names" ou something else.
                      Data is shown as an array of strings, which is not ideal yet.

                      Code:
                      ["user","admin"]
                      Anyway, that's an advancement.

                      Thank you.

                      Comment

                      Working...