问题
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