1758701 Members
2114 Online
108874 Solutions
New Discussion юеВ

egrep issue

 
SOLVED
Go to solution
eric davis_1
New Member

egrep issue

hello all,
so i got the unfortunate task of
debugging a former employees code
and came across an unusual piece that
has me quite complexed (not used to this - im an GUI developer by trade but cutbacks leave us in new worlds).

can anyone here, enlighten me as to what this is trying to match?

^(a(cj)+)\1*$


any suggestions or thoughts?
spent all day discussing it with another developer and we cant figure what its trying to match.

4 REPLIES 4
Rodney Hills
Honored Contributor
Solution

Re: egrep issue

This would match a line

acjcjcjcjacjcjcjcjcjcj

Basically an "a" followed by a string of "cj" repeated. The "\1" means repeat from the first "(" or "a(cj)+".

HTH

-- Rod Hills
There be dragons...
Rodney Hills
Honored Contributor

Re: egrep issue

A detail breakdown-

^ Matches start of line
() A grouping definition
+ Matches at least one or more of pattern
\1 Repeats on first () enclosed pattern
* Matches zero or more of pattern
$ Matches end of line

The smallest match would be-
acj

HTH

-- Rod Hills
There be dragons...
eric davis_1
New Member

Re: egrep issue

ok so 1 a followed by 1 or more cj's
and repeated?

the number of cj's between a's can be
1 or greater?

the * doesnt encompass the entire expression? allowing for an empty string

and the \1 applies to the entire expression
a(cj) not just the a?

so since the entire expression
(a(cj)+) is followed by the \1
it is referring to the entire thing
not just the a

if it were \2 then it would refer only to
the (cj)+ part correct?


i think this means im winning a lunch
Rodney Hills
Honored Contributor

Re: egrep issue

Yes \1 is the entire (a(cj)+). And yes \2 would refer only to (cj). The * binds tight to the \1, so only zero or more of the whole thing will be generated.

With out the \1 their, the pattern could match an empty line also.

HTH

-- Rod Hills
There be dragons...