正则表达式,grep行仅包含1个字符 [英] Regex, grep lines containing only 1 occurrences of a char

查看:269
本文介绍了正则表达式,grep行仅包含1个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个有效的正则表达式(最好是所有格),我可以用于从大文件(5Gb)中仅包含一个定界符(',')的grep行:

I am looking for an efficient regular expression (preferably possessive), which i can use to grep lines containing only one delimiter (',') from a big file (5Gb) :

E.G

X,Y
X1,Y1,Y2
X3,Y3
X4,Y4
X5,Y5,Z6

>>> grep"???" big_file

>>> grep "???" big_file

X,Y
X3,Y3
X4,Y4

推荐答案

尽管@Rawling(这里的答案之一)是正确的,并且他的正则表达式是正确的,但它仍然没有占有欲,因此没有进行优化,但他是正确的不会发生回溯,但是它不会具有最佳性能,因为所有格量词不必记住任何回溯位置.如问题所附链接中所述.

Although @Rawling (one of the answers here) is right and his regular expression is correct, it is still not possessive and therefore not optimized, he is correct that no backtracking will occur, but it will not be with the best performance because the possessive quantifier doesn't have to remember any backtracking positions. as was mentioned in the link attached in the question.

下面的表达式将是所有格和优化的,我将按照问题中的要求与grep一起使用进行演示:

The following expression will be possessive and optimized, i will demonstrate together with the use of grep as was asked in the question:

grep -E"^ [^,] * +,[^,] * + $"大数据

grep -E "^[^,]*+,[^,]*+$" big_data

这篇关于正则表达式,grep行仅包含1个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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