Regex, grep lines containing only 1 occurrences of a char

被刻印的时光 ゝ 提交于 2020-05-08 04:50:42

问题


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

X,Y
X3,Y3
X4,Y4

回答1:


Shouldn't a simple ^[^,]*,[^,]*$ avoid backtracking, because of the start/end-of-string markers?




回答2:


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.

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 "^[^,]*+,[^,]*+$" big_data



来源:https://stackoverflow.com/questions/13418104/regex-grep-lines-containing-only-1-occurrences-of-a-char

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!