Home > Mobile >  delete all characters in each line after the 3rd character using regex in vscode
delete all characters in each line after the 3rd character using regex in vscode

Time:02-08

So I have several lines of text on vscode like so:

Robert Metcalfe, a member of the research staff for Xerox, develops Ethernet for connecting multiple computers and other hardware.

I want to only keep the text from the 10th character and before, so like this:

Robert Met

I saw on another post that ^.{0,10} would be the way to cut the first 10 characters using regex, but how would you cut all characters except the first 10? I wish vscode had some sort of reverse search.

CodePudding user response:

Do a find and replace with a capture group, in regex mode:

Find:    ^(.{10}).*$
Replace: $1

The above captures the first 10 characters in $1, and then replaces the entire line with just this capture group. Here is a demo.

CodePudding user response:

Find: (?<=^\w{10}).*
Replace: with nothing

Using a positive lookbehind means you don't need a capture group or replacement since you are not actually matching anything you want to keep. You are only matching the rest of the line you don't want. See also https://stackoverflow.com/a/71012130/836330

  •  Tags:  
  • Related