简体   繁体   English

"删除每行除前四个字符之外的所有字符"

[英]Remove all but the first four characters on each line

So I have a text file in Vscode that contains several lines of text like so:所以我在 Vscode 中有一个文本文件,其中包含几行文本,如下所示:

1801: Joseph Marie Jacquard, a French merchant and inventor invent a loom that uses punched wooden cards to automatically weave fabric designs. Early computers would use similar punch cards.

So now I'm trying to isolate the year number/the first 4 characters of each line.所以现在我试图隔离年份编号/每行的前 4 个字符。 I'm new to regex, and I know how to get the first 4 characters (I used ^.{4}) but how would I be able to find all EXCEPT for the first 4 characters so that I can replace them with nothing and be left with just the year numbers?我是正则表达式的新手,我知道如何获取前 4 个字符(我使用了 ^.{4}),但是我如何才能找到前 4 个字符除外的所有字符,以便我可以用任何内容替换它们只剩下年份数字?

You can你可以

Find : ^(.{4}).+查找^(.{4}).+
Replace : $1替换$1

See the regex demo .请参阅正则表达式演示 Details :详情

  • ^ - start of a line (in Visual Studio Code, ^ matches any line start) ^ - 行首(在 Visual Studio Code 中, ^匹配任何行首)
  • (.{4}) - capturing group #1 that captures any four chars other than line break chars (.{4}) - 捕获组 #1,捕获除换行符以外的任何四个字符
  • .+ - one or more chars other than line break chars, as many as possible. .+ - 一个或多个除换行符以外的字符,尽可能多。

The $1 backreference in the replacement pattern replaces the match with Group 1 value.替换模式中的$1反向引用将匹配替换为组 1 值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM