nik
nik

Reputation: 2584

deleting all characters after a semicolon

I am using sublime on Mac I have something like this

Q21162;E5QCE7
E5QCF5;P91040
E5QCF6;E5QCF8;P34558
E5QCG0;Q09499
E5QCG2

I want to get rid of everything after the first ; so I want the output like this

Q21162
E5QCF5
E5QCF6
E5QCG0
E5QCG2

Upvotes: 7

Views: 18455

Answers (3)

Gianluca Ghettini
Gianluca Ghettini

Reputation: 11638

regular expression is your best friend

forget about the IDE you are using and do some research on how to use regexes.

Or you can use the built-in regular expression parser included in sublime:

https://docs.sublimetext.io/guide/usage/search-and-replace.html

this is the regex you are looking for:

s/[^;]*$//

I'm assuming you want to remove the text after the last occurrence of a semicolon in a line

Upvotes: 0

Peaceand
Peaceand

Reputation: 296

An alternative to using regular expressions would be using the multi cursor editor to select all the semicolons, then highlight to end of line, then delete.

Using the exact example in the question, this worked perfectly for me:

  1. Highlight the first ;
  2. Press Ctrl + d a few times to highlight the other instances of it too.
  3. Hold Shift and tap End to highlight to the end of the row.
  4. Press Delete

Done using Sublime 3 on Debian - I'm assuming this'll work elsewhere too?

Regular expressions are wonderful, but overkill in this case where a couple of keyboard shortcuts will do the job nicely.

Upvotes: 6

pwagner
pwagner

Reputation: 1244

The following worked for me with a regular expression:

In Sublime:

  1. Open the replace panel with CTRL + H
  2. Make sure that regular expressions are enabled. It is the left icon ".*" in the same row as the search field (ALT + R).
  3. Enter in "Find What": ^([^;]*);.*$
  4. Enter in "Replace With": \1
  5. Find and replace all

I tested it on Windows, on Mac you probably have to use the CMD instead of the CTRL key.

Upvotes: 28

Related Questions