Hello, I have two questions. I apologise if I haven't explained this properly, this is for learning/understanding how regular expressions work.
Could you explain this please;
Quote:
grep "([^()]*)a" file returns any line containing a pair of parentheses that are innermost and are followed by the letter "a". So it matches these lines ;
(hello)a
(aksjdhaksj d ka)a
But not this
x=(y+2(x+1))a
From <http://www.panix.com/~elflord/unix/grep.html>
|
1. ([^()]*) -
My understanding is; this is a paranthesis() around a subexpression. [] brackets matches
any one character inside it, which are "()", the ^ is not a character, but a carat to indicate that "(" or ")" have to be at the beginning of the line.
The problem is, it will return (hello), (hi), but will not return )hello, )hello(. The question is, why does it have to be in that order, and why do both have to be included [ie. is my understanding of brackets wrong]?
2. What is the * for (I understand it means match none or any, so test* will match
test, testing, tested, tester)? I mean, the expression matches anything inside () that ends with an "a" on the outside - so (testing)a, (whats up)abz, (kk)a. So what's the purpose of the *, and why it is outside the brackets, but inside the paranthesis subexpression?
Thanks for any explanation.