Ad Widget

Collapse

Help with Regex - Regular Expression adding leading zero to MAC address

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SpaceMonkey
    Junior Member
    • Nov 2018
    • 6

    #1

    Help with Regex - Regular Expression adding leading zero to MAC address

    I'm monitoring a UPS with zabbix via SNMP. And when I read the MAC address i get it like 0:3:xx:xx:xx:xx i want it like 00:03:xx:xx:xx:xx. I tried it with regex formula in Regular expression with Preproscesing but I can't get it to work. My formula:
    Code:
    /\b[0-9,a-f]\b/g
    works on regex101.com but not on zabbix, also I can't use the /g flag. I can't also use 0$0 because zabbix just writes 0$0 instead of 00:03:xx:xx:xx:xx
  • Mike2K
    Member
    • Oct 2018
    • 62

    #2
    Why so difficult ? Just match the entire value, assuming your check is correct:
    Code:
    /(.*)
    And then do 0$0

    Regular expression tester with syntax highlighting, explanation, cheat sheet for PHP/PCRE, Python, GO, JavaScript, Java, C#/.NET, Rust.

    Comment


    • SpaceMonkey
      SpaceMonkey commented
      Editing a comment
      That adds a zero at the beginning but I want to add 0 at all places where there is single digit, see in my example 0:3:xx --> 00:03:xx
  • Mike2K
    Member
    • Oct 2018
    • 62

    #3
    Ahh, I didn't see that, sorry for that.

    I'm not sure if preprocessing allows this form of iteration. What you could do is create a separate preprocessing rule for each block of the MAC address.
    That won't be an elegant solution but if it works...

    Comment

    • SpaceMonkey
      Junior Member
      • Nov 2018
      • 6

      #4
      No problem and thank you for the idea. But that is also a part of the problem that 0$0 doesn't work as regex but like plain text. So instead of writing the MAC address it writes 0$0 in plain text on latest data tab.

      Comment


      • Mike2K
        Mike2K commented
        Editing a comment
        Wasn't it something like a \0 for all matches and \1, \2 for individual matches ?

        Some reading: http://www.schatenseite.de/en/2018/0...r-expressions/
        Last edited by Mike2K; 07-11-2018, 09:40.

      • SpaceMonkey
        SpaceMonkey commented
        Editing a comment
        Yes \0 also works but zabbix doesn't support global (/g) flag so \0 is the same as \1 in this example because is finds 1st matching and not all. So I would still have to do it for each block.
    • Semiadmin
      Senior Member
      • Oct 2014
      • 1625

      #5
      For example:

      Click image for larger version

Name:	pre.JPG
Views:	842
Size:	24.2 KB
ID:	368583

      Comment


      • SpaceMonkey
        SpaceMonkey commented
        Editing a comment
        Thanks, it works. It's just what I was looking for.
    • SpaceMonkey
      Junior Member
      • Nov 2018
      • 6

      #6
      Thank you Mike2K and Semiadmin for help.

      Comment

      Working...