• Login
Community
  • Login

How do i replace only digits attached with letters

Scheduled Pinned Locked Moved General Discussion
10 Posts 5 Posters 2.8k 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.
  • E
    Ebenezer Amoako Yirenkyi
    last edited by Ebenezer Amoako Yirenkyi May 8, 2020, 6:56 AM May 8, 2020, 6:53 AM

    i have
    PTH 284 Wo, mm4b chunu Awurade
    and i want to change only the 4 within the string without affect the 284
    so i want to change 4 to e
    so the result i need is
    PTH 284 Wo, mmeb chunu Awurade

    E 1 Reply Last reply May 8, 2020, 11:19 AM Reply Quote 0
    • E
      Ekopalypse @Ebenezer Amoako Yirenkyi
      last edited by Ekopalypse May 8, 2020, 11:22 AM May 8, 2020, 11:19 AM

      @Ebenezer-Amoako-Yirenkyi

      given your requirement one possible solution would be to
      search for \d(?=[a-zA-Z]) , replace with e and
      having search mode regular expression checked.

      If the requirement is that the digit is surrounded by letters, then
      search for (?<=[a-zA-Z])\d(?=[a-zA-Z])

      E 1 Reply Last reply May 12, 2020, 5:56 AM Reply Quote 1
      • G
        guy038
        last edited by guy038 May 8, 2020, 11:25 AM May 8, 2020, 11:24 AM

        ello @ebenezer-amoako-yirenkyi and All,

        To change any number, embedded in letters of a word, use the following regex S/R :

        • Open the Replace dialog ( Ctrl + F )

        -SEARCH (\p{alpha})\d+(?=\p{alpha})

        • REPLACE ${1}Your replacement ( So ${1}e, in your case )

        • Tick the Wrap around option, if necessary

        • Select the Regular expression search mode

        • Click once the Replace All button or several times on the Replace button

        Best Regards

        guy038

        A E 2 Replies Last reply May 8, 2020, 12:00 PM Reply Quote 2
        • A
          Alan Kilborn @guy038
          last edited by May 8, 2020, 12:00 PM

          @guy038 said in How do i replace only digits attached with letters:

          Open the Replace dialog ( Ctrl + F )

          Of course you meant Ctrl + h for this.

          Nice use of \p{alpha} from the Character Properties table on this PAGE which has a link that lead to HERE and then finally HERE to find “alpha”.

          Perhaps \p{space} will start getting more use as a more “readable” way of searching for whitespace? Although I’d have to go off and figure out exactly what types of whitespace it matches. :-)

          I also noted your use of ${1} in the replacement, rather than the usual \1. I think that is a good thing, because it looks “cleaner”, it features no “leaning toothpick”, and it works when the number part is > 9.

          P 1 Reply Last reply May 8, 2020, 5:42 PM Reply Quote 2
          • P
            PeterJones
            last edited by May 8, 2020, 5:17 PM

            @Alan-Kilborn said in How do i replace only digits attached with letters:

            Nice use of \p{alpha} from the Character Properties table on this PAGE which has a link that lead to HERE and then finally HERE to find “alpha”.

            Or you could go to our documentation on character properties which direct you to the big old table of valid names right above. :-)

            A 1 Reply Last reply May 8, 2020, 5:27 PM Reply Quote 3
            • A
              Alan Kilborn @PeterJones
              last edited by May 8, 2020, 5:27 PM

              @PeterJones said in How do i replace only digits attached with letters:

              Or you could go to our documentation on character properties

              Yea, since “our” docs on this are pretty much a verbatim copy of the original, I like to go back to the original, because of the “typo” potential in the copy/edit. :-)

              1 Reply Last reply Reply Quote 2
              • P
                PeterJones @Alan Kilborn
                last edited by PeterJones May 8, 2020, 5:43 PM May 8, 2020, 5:42 PM

                @Alan-Kilborn said in How do i replace only digits attached with letters:

                Although I’d have to go off and figure out exactly what types of whitespace it matches. :-)

                Actually, @guy038 did that work back in November: [\t\n\x0B\f\r\x20\x85\xA0\x{2028}\x{2029}]
                (merged in PR#59 ) …

                edit: and you would have seen that if you used “ours” instead of “theirs” ;-)

                1 Reply Last reply Reply Quote 2
                • G
                  guy038
                  last edited by guy038 May 12, 2020, 12:42 PM May 8, 2020, 7:53 PM

                  Hi @ebenezer-amoako-yirenkyi @alan-kilborn, @peterjones and All,

                  Alan, I used the ${1} syntax because I was afraid that, in case you would have preferred to change the 4 digit, by any number, this would not work.

                  Luckily, I realize that I was wrong because, for instance, the syntax \123, in replacement, always means change current match with contents of group 1, ( even if not defined => an empty string), followed with the number 23

                  So, the two replacement syntaxes \1Your replacement or ${1}Your replacement are safe ! Unlike the third syntax $1Your replacement which is best to avoid when your replacement is a number !

                  BR

                  guy038

                  1 Reply Last reply Reply Quote 2
                  • E
                    Ebenezer Amoako Yirenkyi @guy038
                    last edited by May 12, 2020, 5:50 AM

                    @guy038 it worked perfectly

                    1 Reply Last reply Reply Quote 1
                    • E
                      Ebenezer Amoako Yirenkyi @Ekopalypse
                      last edited by May 12, 2020, 5:56 AM

                      @Ekopalypse it worked perfectly thanks for the help

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