Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

what the different between these two regex expression ?

AB\{4\} and (AB)\{4\}

for the first expression I think that it repeat B 4 times, then for second expression it

repeats AB 4 times. When I try to test the second expression using ABABABAB nothing matches.

share|improve this question
    
What tool are you using as a regex engine? –  Joseph R. Mar 20 '13 at 23:12
    
I just tested in bash by creating a file and then use grep command –  user1763658 Mar 20 '13 at 23:14
    
Try escaping the braces with a backslash –  Joseph R. Mar 20 '13 at 23:18
    
sorry that was in the original question, I guess need to put \\ in order to show the backslash on stack exchange –  user1763658 Mar 20 '13 at 23:20
3  
Write it (AB){4} if it's a ERE, \(AB\)\{4\} if it's a BRE. –  Stephane Chazelas Mar 20 '13 at 23:22
add comment

1 Answer

up vote 5 down vote accepted

grep by default uses BREs, so you need to escape your parentheses:

grep 'AB\{4\}'
grep '\(AB\)\{4\}'

You can also use EREs by passing the -E option, available on some grep implementations:

grep -E 'AB{4}'
grep -E '(AB){4}'
share|improve this answer
    
For what it's worth, -E is specified by POSIX, and grep -E is sometimes available as egrep (although this invocation is now deprecated). –  rahmu Mar 20 '13 at 23:43
    
@rahmu POSIX still doesn't cover the entire range of Unices, and egrep isn't specified by POSIX. –  Chris Down Mar 21 '13 at 0:05
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.