Community
    • Login

    Find and Replace characters in a column range using Regex

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    3 Posts 3 Posters 3.1k 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.
    • Bob JohnsonB Offline
      Bob Johnson
      last edited by

      I need to check columns 50 thru 55 for a - and if found replace with a . My file has 1M lines.

      I have figured out how to start it for column 50 but how do i repeat this for the next 4 columns.

      Find what: ^.{49}\K-
      Replace with: .

      1 Reply Last reply Reply Quote 0
      • Terry RT Offline
        Terry R
        last edited by

        @Bob-Johnson said in Find and Replace characters in a column range using Regex:

        how do i repeat this for the next 4 columns

        The simplest way would be to use your original regex, but change the 49 to 50, then 51 etc. So in effect you run the regex 5 times in total, increasing the number by 1 each time.

        It is possible to create another regex to cater for 1,2,3,4 or 5 - in a row that only needs running once, but I don’t think it’s worth the effort, especially if what you are editing is not a regular occurrence.

        Terry

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

          Hello, @bob-johnson, @terry-r and All,

          OK, Bob. Luckily your S/R does not change the columns order, as you just want to replace 1 char with a single char, too. So, given the sample text below :

                                                           Column
                                                         4455555555
                                                         8901234567
          0       ABCDE                     VWXYZ        -                12345    # - at column 48
          0       ABCDE                     VWXYZ         -               12345    # - at column 49
          
          0       ABCDE                     VWXYZ          -BCDEFG        12345    # - at column 50
          0       ABCDE                     VWXYZ          A-CDEFG        12345    # - at column 51
          0       ABCDE                     VWXYZ          AB-DEFG        12345    # - at column 52
          0       ABCDE                     VWXYZ          ABC-EFG        12345    # - at column 53
          0       ABCDE                     VWXYZ          ABCD-FG        12345    # - at column 54
          0       ABCDE                     VWXYZ          ABCDE-G        12345    # - at column 55
          
          0       ABCDE                     VWXYZ                -        12345    # - at column 56
          0       ABCDE                     VWXYZ                 -       12345    # - at column 57
          

          The following regex S/R :

          • SEARCH (?-s)^.{49}.{0,5}\K-

          • REPLACE .

          • Tick the Wrap around option

          • Click on the Replace All button

          • And choose, of course, the Regular expression search mode

          It should modify the text, as expected :

                                                           Column
                                                         4455555555
                                                         8901234567
          0       ABCDE                     VWXYZ        -                12345    # - at column 48
          0       ABCDE                     VWXYZ         -               12345    # - at column 49
          
          0       ABCDE                     VWXYZ          .BCDEFG        12345    # - at column 50
          0       ABCDE                     VWXYZ          A.CDEFG        12345    # - at column 51
          0       ABCDE                     VWXYZ          AB.DEFG        12345    # - at column 52
          0       ABCDE                     VWXYZ          ABC.EFG        12345    # - at column 53
          0       ABCDE                     VWXYZ          ABCD.FG        12345    # - at column 54
          0       ABCDE                     VWXYZ          ABCDE.G        12345    # - at column 55
          
          0       ABCDE                     VWXYZ                -        12345    # - at column 56
          0       ABCDE                     VWXYZ                 -       12345    # - at column 57
          

          Notes :

          • The first part (?-s) forces the regex engine to consider that dot ( . ) represents a single standard char only ( Not an EOL one )

          • The special syntax \K, cancels any previous search, already matched, at current location of the regex engine and, in your case, just consider the last - character

          Best Regards,

          guy038

          1 Reply Last reply Reply Quote 3

          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