Community
    • Login

    regex: add specific lines if detect more than one hexadecimal digit

    Scheduled Pinned Locked Moved General Discussion
    6 Posts 4 Posters 553 Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • lightning speedL
      lightning speed
      last edited by

      Hi there,
      I have the multi-lines shown below and would like to copy the 3 lines Portindex, Portname & PortState from above if there is more than one hexadecimal digit separated with a colon char.
      No changes if there is only one hexadecimal digit or no hexadecimal digit. Thanks in advance.

      From this:
      portIndex: 130
      portName: slot1 port18
      portState: 1Online
      10:00:00:90:fe:c0:60:91

      portIndex: 131
      portName: slot1 port19
      portState: 1Online
      10:00:00:10:3e:fa:65:88
      10:00:00:10:3f:fb:66:99
      21:00:00:1a:1b:b5:9b:80

      portIndex: 132
      portName: slot1 port20
      portState: 1Online
      10:00:00:10:4a:bb:d9:6e
      10:00:00:10:4a:ba:52:b9

      portIndex: 243
      portName: slot12 port19
      portState: 2Offline

      To this:
      portIndex: 130
      portName: slot1 port18
      portState: 1Online
      10:00:00:90:fe:c0:60:91

      portIndex: 131
      portName: slot1 port19
      portState: 1Online
      10:00:00:10:3e:fa:65:88
      portIndex: 131
      portName: slot1 port19
      portState: 1Online
      10:00:00:10:3f:fb:66:99
      portIndex: 131
      portName: slot1 port19
      portState: 1Online
      21:00:00:1a:1b:b5:9b:80

      portIndex: 132
      portName: slot1 port20
      portState: 1Online
      10:00:00:10:4a:bb:d9:6e
      portIndex: 132
      portName: slot1 port20
      portState: 1Online
      10:00:00:10:4a:ba:52:b9

      portIndex: 243
      portName: slot12 port19
      portState: 2Offline

      Terry RT 1 Reply Last reply Reply Quote 0
      • Terry RT
        Terry R @lightning speed
        last edited by

        @lightning-speed said in regex: add specific lines if detect more than one hexadecimal digit:

        if there is more than one hexadecimal digit separated with a colon char.

        That statement in ambiguous since all records you have shown have more than 1 hexadecimal digit. That is if you regard the meaning of “digit”. Digit means 1 character of a numeric type.

        I suspect what you really meant was more than 1 consecutive line containing hexadecimal numbers of the following format ##:##:##:##:##:##:##:##.

        Please read the post pinned to the start called “Please Read this before posting”. Then respect the need to properly provide examples, both before and after in the correct manner.

        All too often questions are posed (as you have done) which are ambiguous or in some cases just dummy data. So when someone spends time trying to help only to find the question raiser has failed to properly show information, it leaves the helper wondering why they even bothered.

        Terry

        lightning speedL 1 Reply Last reply Reply Quote 2
        • lightning speedL
          lightning speed @Terry R
          last edited by

          @Terry-R
          Thanks for your advice. What I meant was no changes if there is only one or no hexadecimal shown below. I should have omitted the word digit. Apologies if I have caused any misinterpretation or misunderstanding.

          Best Regards

          PeterJonesP 1 Reply Last reply Reply Quote 0
          • PeterJonesP
            PeterJones @lightning speed
            last edited by PeterJones

            @lightning-speed ,

            That still might not be sufficient information for us to help you.

            The pinned post that @Terry-R mentioned will direct you to pages on how to ask good questions and how to format posts so that the data comes through correctly. If you refuse to follow that advice, then the best you will get is our guesses as to what you want, which will often result in way too many back-and-forths between you and the people who are willing to make those guesses.

            ----

            Useful References

            • Please Read Before Posting
            • Template for Search/Replace Questions
            • Formatting Forum Posts
            • FAQ: Where to find regular expressions (regex) documentation
            • Notepad++ Online User Manual: Searching/Regex
            1 Reply Last reply Reply Quote 2
            • guy038G
              guy038
              last edited by guy038

              Hello, @lightning-speed, @terry-r and All

              Here is a possible solution which will need two consecutive regex S/R !

              • First add a few line-breaks at the very end of your file !

              Then, from this INPUT text :

              portIndex: 130
              portName: slot1 port18
              portState: 1Online
              10:00:00:90:fe:c0:60:91
              
              portIndex: 131
              portName: slot1 port19
              portState: 1Online
              10:00:00:10:3e:fa:65:88
              10:00:00:10:3f:fb:66:99
              21:00:00:1a:1b:b5:9b:80
              
              portIndex: 132
              portName: slot1 port20
              portState: 1Online
              10:00:00:10:4a:bb:d9:6e
              10:00:00:10:4a:ba:52:b9
              
              portIndex: 243
              portName: slot12 port19
              portState: 2Offline
              

              If we use this first regex S/R :

              SEARCH (?-s)^((?:.+\R){3})((?:(?:^[[:xdigit:]:]+)\R)+)|^((?:.+\R){3})\R

              REPLACE ?1\2\1:No\r\n\3

              you’ll get this temporary OUTPUT text :

              10:00:00:90:fe:c0:60:91
              portIndex: 130
              portName: slot1 port18
              portState: 1Online
              
              10:00:00:10:3e:fa:65:88
              10:00:00:10:3f:fb:66:99
              21:00:00:1a:1b:b5:9b:80
              portIndex: 131
              portName: slot1 port19
              portState: 1Online
              
              10:00:00:10:4a:bb:d9:6e
              10:00:00:10:4a:ba:52:b9
              portIndex: 132
              portName: slot1 port20
              portState: 1Online
              
              No
              portIndex: 243
              portName: slot12 port19
              portState: 2Offline
              

              => This first regex

              • Write the lines containing hexadecimal characters first, followed with the 3 lines of code of the current section

              • Inserts a No line, followed with the 3 lines of code, if the current section does not contain any line with hexa chars

              Then using this second regex S/R :

              SEARCH (?:((?:^[[:xdigit:]:]+)\R)|^No\R)(?=(?s:.*?^(portIndex.+?\R)\R))|(?s)^portIndex.+?\R(?=\R)

              REPLACE \2?1\1

              You should obtain the expected OUTPUT text :

              portIndex: 130
              portName: slot1 port18
              portState: 1Online
              10:00:00:90:fe:c0:60:91
              
              portIndex: 131
              portName: slot1 port19
              portState: 1Online
              10:00:00:10:3e:fa:65:88
              portIndex: 131
              portName: slot1 port19
              portState: 1Online
              10:00:00:10:3f:fb:66:99
              portIndex: 131
              portName: slot1 port19
              portState: 1Online
              21:00:00:1a:1b:b5:9b:80
              
              portIndex: 132
              portName: slot1 port20
              portState: 1Online
              10:00:00:10:4a:bb:d9:6e
              portIndex: 132
              portName: slot1 port20
              portState: 1Online
              10:00:00:10:4a:ba:52:b9
              
              portIndex: 243
              portName: slot12 port19
              portState: 2Offline
              

              => This second regex :

              • Adds the 3 lines of code, of the current section, before repeating each line of hexadecimal characters, if any

              • Deletes the temporary 3 lines of code at the end of each section

              Best Regards,

              guy038

              lightning speedL 1 Reply Last reply Reply Quote 0
              • lightning speedL
                lightning speed @guy038
                last edited by

                @guy038

                The regex you provided works great. Many thanks for your assistance. ChEEr!!!

                Best Regards

                1 Reply Last reply Reply Quote 1
                • First post
                  Last post
                The Community of users of the Notepad++ text editor.
                Powered by NodeBB | Contributors