Community
    • Login

    Finding and replacing characters

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    7 Posts 3 Posters 865 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.
    • 2 mins Summary2
      2 mins Summary
      last edited by

      Hi everyone,

      I am struggling to find a way to replace characters for sorting purpose.

      for example, I want to replace the following characters:

      A36
      A282
      A1482

      into:
      A0036
      A0282
      A1482

      where one zero is added after the letter “A” whenever there are three numbers and two zeros are added after “A” whenever there are three numbers.

      Also, nothing should be done if there are four numbers after “A”.

      I really appreciate it if you can help me.

      PeterJonesP 1 Reply Last reply Reply Quote 0
      • PeterJonesP
        PeterJones @2 mins Summary
        last edited by PeterJones

        @2-mins-summary ,

        You will need to use capture groups and conditional substitutions. I have also added negative lookahead and negative lookbehind assertions, though it can probably be done without them, because I wanted to limit it to 1-4 digits.

        You weren’t 100% clear as to whether these 1-4 digits would always be after an A, or always after a letter, or always starting in the second column of the line, or some combination, so I made mine restrict it to any group of 1-4 digits, anywhere on the line, as long as there isn’t a digit before or a digit after (which would have meant it was really 5+ digits).

        FIND = (?<!\d)\d(\d)?(\d)?(\d)?(?!\d)
        REPLACE = (?1:0)(?2:0)(?3:0)$0
        SEARCH MODE = Regular Expression

        This converted

        A1
        A36
        A282
        A1482
        

        into

        A0001
        A0036
        A0282
        A1482
        

        Basically, the logic puts each individual digit after the first digit into a numbered group (1-3), as long as it’s exactly 1 thru exactly 4 digits with non-digits surrounding; then the replacement will say “if there isn’t a digit for the Nth group, use a 0 at the beginning instead” 3 times, for the three optional digits – so it will insert no 0s before a four-digit number, one 0 before a three-digit number, two 0 before a two-digit number, and three 0 before a one-digit number.

        If you need it to be more restrictive, you will need to be more clear about examples that shouldn’t match; if you need it to be more permissive, you need to give more examples of data that should match that my expression does not. In other words, my answer is only as good as the example you gave and the assumptions I made (intentionally or unknowingly) based on the example data you provided.

        ----

        Useful References

        • Please Read Before Posting
        • Template for Search/Replace Questions
        • FAQ: Where to find regular expressions (regex) documentation
        • Notepad++ Online User Manual: Searching/Regex

        –
        edit: fixed typos in the original FIND

        2 mins Summary2 1 Reply Last reply Reply Quote 1
        • 2 mins Summary2
          2 mins Summary @PeterJones
          last edited by

          @peterjones Really appreciate it.

          I mean it always should be after an A.

          Could you please help me with it?

          Alan KilbornA PeterJonesP 2 Replies Last reply Reply Quote 0
          • Alan KilbornA
            Alan Kilborn @2 mins Summary
            last edited by

            @2-mins-summary said in Finding and replacing characters:

            it always should be after an A

            A quick mod to Peter’s for this would be to start off his Find expression with A\K

            1 Reply Last reply Reply Quote 3
            • PeterJonesP
              PeterJones @2 mins Summary
              last edited by

              @2-mins-summary

              it always should be after an A

              FIND = (?<=A)\d(\d)?(\d)?(\d)?(?!\d)

              Everything else stays the same

              I just had to change the negative lookbehind that said “anything that doesn’t contain a digit” to be a positive lookbehind that said “only match if the character before is an A”

              A1
              B9
              A12
              B98
              A123
              B987
              A1234
              B9876
              A12345
              B98765
              

              becomes

              A0001
              B9
              A0012
              B98
              A0123
              B987
              A1234
              B9876
              A12345
              B98765
              

              So the B’s don’t change but the A’s do

              2 mins Summary2 1 Reply Last reply Reply Quote 3
              • 2 mins Summary2
                2 mins Summary
                last edited by

                for example:

                Team A1 54
                Team A36 63
                Team A282 88
                Team A1482 98

                into:
                Team A0001 54
                Team A0036 63
                Team A0282 88
                Team A1482 98

                Where where zeros are added after letter A always if the numbers are less than 4.

                Thank you

                1 Reply Last reply Reply Quote 0
                • 2 mins Summary2
                  2 mins Summary @PeterJones
                  last edited by

                  @peterjones Thank you very much

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