仅解析具有多个匹配项的行中的第一个正则表达式匹配项 [英] Parsing only first regex match in a line with several matches

查看:176
本文介绍了仅解析具有多个匹配项的行中的第一个正则表达式匹配项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以有一个仅从该行a1bcdea1ABCa1DEFa1解析a1bcdea1的正则表达式?

Is it possible to have a regex that parses only a1bcdea1 from this line a1bcdea1ABCa1DEFa1 ?

此grep命令不起作用:

This grep command does not work:

$ cat txtfile
a1bcdea1ABCa1DEFa1
$ grep -oE "[A-Z,a-z]1.*?[A-Z,a-z]1" txtfile
a1bcdea1ABCa1DEFa1

我希望grep的输出仅为a1bcdea1.

I want the output of grep to be only a1bcdea1.

很明显,我可以在上面的行中使用grep -o"a1bcdea1",但是要考虑一个行是否有几千行,并且目标是每行都匹配第一个[A-Z,a-z]1.*?[A-Z,a-z]1.

It is obvious that I can just use grep -o "a1bcdea1" for the above line, but consider if one has several thousands of lines and the goal is to match FIRST [A-Z,a-z]1.*?[A-Z,a-z]1 for each single line.

推荐答案

如何使用 ^开始锚点并限制使用的字符集:

How about using a ^ start anchor and restricting character set used:

grep -o '^[A-Za-z]1[A-Za-z]*1'

>或如果您希望两者之间有更多的数字或其他字符,请使用与此

If you expect more digits or other characters in between, go with this

grep -oP '^[A-Za-z]1.*?[A-Za-z]1'

惰性匹配要求与此

The lazy matching requires perl compatible mode. For not at line start, go with this

grep -oP '^.*?\K[A-Za-z]1.*?[A-Za-z]1'

\K重置报告的匹配开始也是PCRE的功能.

\K resets beginning of the reported match and is a PCRE feature as well.

这篇关于仅解析具有多个匹配项的行中的第一个正则表达式匹配项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆