简体   繁体   English

正则表达式只获取每行的第一个单词

[英]Regular expression to get only the first word from each line

I have a text file我有一个文本文件

@sp_id      int,    
@sp_name                varchar(120),
@sp_gender              varchar(10),
@sp_date_of_birth       varchar(10),
@sp_address             varchar(120),
@sp_is_active           int, 
@sp_role            int

Here, I want to get only the first word from each line.在这里,我只想从每一行中获取第一个单词。 How can I do this?我怎样才能做到这一点? The spaces between the words may be space or tab etc.单词之间的空格可以是空格制表符等。

Here is what I suggest:这是我的建议:

Find what : ^([^ \\t]+).*找出什么^([^ \\t]+).*

Replace with : $1替换为$1

Explanation : ^ matches the start of line, ([^ \\t]+) matches 1 or more (due to + ) characters other than space and tab (due to [^ \\t] ), and then any number of characters up to the end of the line with .* .说明^匹配行首, ([^ \\t]+)匹配 1 个或多个(由于+spacetab符以外的字符(由于[^ \\t] ),然后匹配任意数量的字符.*行的结尾。

See settings:查看设置:

在此处输入图片说明

In case you might have leading whitespace, you might want to use如果您可能有前导空格,您可能需要使用

^\s*([^ \t]+).*

Find What: ^(\\S+).*$查找内容: ^(\\S+).*$

Replace by : \\1替换为: \\1

You can simply use this to get the first word.Here we are capturing the first word in a group and replace the while line by the captured group.您可以简单地使用它来获取第一个单词。这里我们正在捕获组中的第一个单词,并用捕获的组替换 while 行。

使用/^\\w+/gm查找每行的第一个单词。

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

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