Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Hi all,
How can I match exactly 2 occurences of zero. It's supposed to be:
0\{2\}, but as you can see it doesn't work as it supposed to. It seems that
it doesn't make any difference between 0\{2,\} - which should find 2 or more occurences of zero and 0\{2\} which is supposed to find exactly 2 occurences of zero.
What am I doing wrong? All the examples find at least 2 zeros. I tried using single quotes (the same results) and no quotes (doesn't work).
That looks like correct behaviour to me. All those lines do contain substrings which are exactly two zeroes - your regex doesn't say that such a substring can't be followed by more zeroes.
'[^0]00[^0]' will match strings of two zeroes with any character other than 0 before and after them.
What you want is probably '(^|[^0])00($|[^0])', which will also match strings of two zeroes at the beginning or end of a line. But note that to use the '|' character with grep, you need to turn on extended mode by calling egrep or using the -e switch. Or you can still use grep's basic mode, but escape the | with a backslash.
That looks like correct behaviour to me. All those lines do contain substrings which are exactly two zeroes - your regex doesn't say that such a substring can't be followed by more zeroes.
Quote:
\{m,n\} Matches the preceding element at least m and not more than n times (from wikipedia)
man grep:
Quote:
{n} The preceding item is matched exactly n times.
{n,} The preceding item is matched n or more times.
{n,m} The preceding item is matched at least n times, but not more than m times.
In my examples everything works as if it were {n,}.
If they just find a substring n and do not care what follows, then what is the point of introducing 3 syntactic variations of the same thing.
hmmm, it's getting a bit funny.
Is there anything peculiar in my situation that it won't work as it's supposed to?
But it is working as it's supposed to! What makes you think 0{2} doesn't match "10000"? It does, in fact it matches it 3 times, in the 2nd, 3rd, and 4th positions.
It might help to realise that:
0{2} is equivalent to 00
0{2,3} is equivalent to 000?, and also equivalent to (00)|(000)
0{2,} is equivalent to 000*
does that make sense now? Do you see why my examples with abbba etc did what you expected, and yours didn't?
The "00" pattern is in "0000000" so you will have a match. In order to match exactly 2 zeroes, you need to take the neighbors that you allow into account. [] is used to produce a set of characters for a match. If the first character is ^, then the meaning is negated, and you want to match any character not in the set. [^0] means any character but "0". Outside the square brackets, ^ means the beginning of a line. $ means the end of a line. [^0]0{2,3}[^0] matches a non-zero character followed by 2 or 3 zeroes, followed by a non-zero character. That will match 4006 or 220005 but not 10000.
I get it now. Initially I thought that by writing {2,3} it will automatically ensure that the pattern of 2-3 zeroes will not be followed by any other zero (or another match of 2-3 zeros). You live, you learn.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.