Search code examples
regexsublimetext3

Sublime: remove part of the row, in all columns


I have a very big text file with multiple lines.

I want to remove a portion of the text file in every line. This portion is at the same place for every line

For example

ABC.com/I_WANT_THIS_DELETED
ABC.com/I_WANT_THIS_DELETED

Repeat for thousand times.

I know i can press *CONTROL + LEFT CLICK, in order to set multiple cursors and mass delete.

But this is extremely impractical, as there are thousands of links.

How can i set a number of characters in the first row, and then select all that part for every row, so i can delete it?


Solution

  • Say you want to delete everything after 20 characters. Open Find → Replace… and enter

    (^.{20})(.*$)
    

    in the Find field and

    \1
    

    in the Replace field. Ensure the Regular Expression and Wrap buttons are selected, then hit Replace or Replace All.

    EXPLANATION

    MATCH
    (^.{20})        first group
     ^              start from beginning of line
      .             match any character
       {20}         20 times exactly
    
            (.*$)   second group
             .      match any character
              *     zero or more times
               $    until the end of the line
    
    REPLACE
    \1              replace with group 1