• Login
Community
  • Login

Find and Replace characters in a column range using Regex

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
3 Posts 3 Posters 2.4k 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.
  • B
    Bob Johnson
    last edited by Oct 25, 2019, 10:49 PM

    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
    • T
      Terry R
      last edited by Oct 26, 2019, 2:41 AM

      @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
      • G
        guy038
        last edited by Oct 26, 2019, 6:01 PM

        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
        1 out of 3
        • First post
          1/3
          Last post
        The Community of users of the Notepad++ text editor.
        Powered by NodeBB | Contributors