简体   繁体   English

如何使用EGREP搜索行中第一次出现的模式

[英]How to search for the 1st occurrence of a pattern in a line using EGREP

I am using EGREP regex to search for some patterns in a file that contains URLs. 我正在使用EGREP正则表达式在包含URL的文件中搜索某些模式。 I want to find the first instance only in each line. 我想在每一行中找到第一个实例。 For example, this is my regex: 例如,这是我的正则表达式:

egrep -io '^\<http(s)://home\>+\..+\.gov(\.au)?' input.txt

It output this instance: 它输出这个实例:

https://home.xxx.gov/uuu.aspx?url=https://home.xxx.gov

But what I really look for in this specific example is: 但是我在这个具体例子中真正寻找的是:

https://home.xxx.gov

I do not care what comes after the .gov and I want to trim it. 我不在乎.gov之后会发生什么,我想修剪它。 How can I do this? 我怎样才能做到这一点?

You'll need a lazy quantifier , and for that you need Perl-style regexes: 你需要一个懒惰的量词 ,为此你需要Perl风格的正则表达式:

egrep -P -io '^https?://home\..+?\.gov(\.au|\.uk)?' input.txt

If your egrep doesn't support Perl regexes, you need to find a different way, for example 如果你的egrep不支持Perl正则表达式,你需要找到一种不同的方式,例如

egrep -io '^https?://home\.[A-Za-z0-9.]+\.gov(\.au|\.uk)?' input.txt

or 要么

egrep -io '^https?://home\.[^/]+\.gov(\.au|\.uk)?' input.txt

limiting the range of characters that may be matched by the regex. 限制正则​​表达式可能匹配的字符范围。 See also @sshashank124's solution. 另见@ sshashank124的解决方案。

你可以这样做:

^\\<https?://home\\.\\w+\\.gov(\\.au|\\.uk)?

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

相关问题 使用正则表达式提取具有特定模式的第一行 - extract 1st line with specific pattern using regexp 使用正则表达式从具有特殊模式的文本中提取第一行 - extract 1st line from a text with special pattern using regexp 如何使用egrep搜索带有逗号和数字的特定模式 - How to search a specific pattern with commas and digits using egrep 如何搜索字符串中第一次出现的“:/”,然后搜索找到的子字符串中所有其他出现的内容,包括“:/”? - How to search a string for 1st occurrence of “:/” and then search all other occurences of the found substring inclusive “:/”? 如何使用正则表达式获取模式后的第一个字符? - How to get the 1st character after a pattern using regex? 如何在带有正则表达式的URL中找到第一个精确字符串 - How to find 1st occurrence of exact string in URL with regular expression 例如,无法使用正则表达式模式将 1st 替换为 first - Cannot replace, for example, 1st with first using a regex pattern Uipath 正则表达式:具有相同单词的多行,抓取直到第一次出现的行 - Uipath Regex: Many lines with same word, grab until line with 1st occurrence pcregrep匹配在第一次出现时停止 - pcregrep matching stop at 1st occurrence 根据关键字提取字符串的第一次出现 - Extract the 1st occurrence of a string based on a keyword
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM