Community
    • Login

    Find and Replace a specific number but not when followed by a specific character

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    3 Posts 3 Posters 1.6k Views 1 Watching
    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.
    • Jesper Bødewadt MøllerJ Offline
      Jesper Bødewadt Møller
      last edited by

      I need to replace a number in different strings.
      eks. #ioStatusInput3.%X31
      Like replace 3 with 4 but not replace any given number in the “.%X31” only if there is any chracter in front the “3” and not replace the .%X31.
      some times .%X31 have 3 in it and some times not.

      Hope some one can help out.
      Jesper

      Alexandra TunsoiuA 1 Reply Last reply Reply Quote 0
      • Alexandra TunsoiuA Offline
        Alexandra Tunsoiu @Jesper Bødewadt Møller
        last edited by

        @Jesper-Bødewadt-Møller Hi! If you want to replace 3 with 4 whitout affecting .%X31 in this example the solution would be:

        Find what: ([#][A-Z][A-Z][A-Z][A-Z][A-Z][A-Z][A-Z][A-Z][A-Z][A-Z][A-Z][A-Z][A-Z])([0-9])
        Replace with: \14

        If this doesn’t help you, i don’t know, try to be more specific and post more examples of strings you need to replace.

        Kind regards!

        1 Reply Last reply Reply Quote 0
        • guy038G Offline
          guy038
          last edited by guy038

          Hi, @Jesper-Bødewadt-Møller,

          I would use this strategy :

          • If we find some digits, right after the syntax %X, just rewrite this NON wanted block, as it !

          • If we find other 3 digits elsewhere ( which are good candidates ), replace each one with a 4 digit

          This simple way avoid us to verify where a 3 digit is found ( inside a %X... block or elsewhere :-))


          So, open the Replace dialog ( Ctrl + H )

          SEARCH (?-i)%X\d+|(3)

          REPLACE ?{1}4:$0

          OPTIONS Wrap around and Regular expression ticked

          ACTION Click on the Replace All button


          For instance, assuming the initial text below :

          abc3.%X31 def abc3%X11223def abc3%X773777.3def abc3%X31 333def abc3% X321 333def abc3%X 321 333def abc333.%X333333.333def
          

          It would be changed into :

          abc4.%X31 def abc4%X11223def abc4%X773777.4def abc4%X31 444def abc4% X421 444def abc4%X 421 444def abc444.%X333333.444def
          

          Remark :

          Just note that the 3 digits are replaced by a 4 digit , in the parts % X321 and %X 321, as they are not exactly of the form %Xdigitdigit…digit !!


          Notes :

          • The modifier (?-i) forces the search to be NON-insensitive to case ( so sensitive ! )

          • Then the regex engine looks for, either :

            • The exact string %X, followed by, at least, one digit ( %X\d+ )

            • Any digit 3, stored as group 1, because of the surrounding parentheses ( (3) )

          • In replacement :

            • If group 1 exists ( second alternative ), we replace with a single 4 digit ( part before the colon )

            • ELSE, we just rewrite the entire matched string %X##### ( regex $0 , after the colon )

          Best Regards,

          guy038

          1 Reply Last reply Reply Quote 0

          Hello! It looks like you're interested in this conversation, but you don't have an account yet.

          Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

          With your input, this post could be even better 💗

          Register Login
          • First post
            Last post
          The Community of users of the Notepad++ text editor.
          Powered by NodeBB | Contributors