Share your knowledge at the LQ Wiki.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 01-20-2014, 03:30 PM   #1
LQ Newbie
Registered: Jan 2014
Posts: 13

Rep: Reputation: Disabled
Grep extended regular expressions

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;

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 ;

(aksjdhaksj d ka)a

But not this

From <>
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.

Last edited by tt1187; 01-20-2014 at 03:32 PM.
Old 01-20-2014, 07:26 PM   #2
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,530

Rep: Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897
Ok ... answers are:

1. As we are NOT using extended matching with grep (ie. you need to either use egrep or supply -E), the brackets are literal, so if we start left to right:
( - - match a left round bracket anywhere in the line

[^()]* - - zero or more characters that are not (^ here means not as opposed to the start of the line, it has this special meaning only when used inside [])

) - - match a right round bracket

a - - match the literal character a
So looking at the example that does not match - - x=(y+2(x+1))a - - the first left round bracket does not encounter its matching right round bracket before matching another left round bracket which
would need to immediately be followed by a right round bracket and an 'a' so grep would move to the next left round bracket to look for a match. And the end of its search it finds a right round bracket
followed by another right round bracket, ie not an 'a', hence no match on the entire line

2. when globbing, ie which is what the bash shell uses for matching, test* will match testing, however when used in a regular expression, the * means zero or more of the preceding character,
so test* will match - - tes, testt, testtttttt, etc

Hope that helps


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Grep regular expressions rahularjun86 Linux - Newbie 5 12-03-2011 04:41 PM
[SOLVED] Regular Expressions and Grep metallica1973 Linux - General 8 03-08-2011 08:17 AM
[SOLVED] Help using wc and grep with regular expressions citygrid Linux - Newbie 12 04-17-2010 02:29 AM
extended regular expressions wakatana Linux - Newbie 12 10-22-2009 01:27 AM
Regular expressions using grep linuxmandrake Programming 3 11-16-2005 04:29 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:35 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration