From: Jeremy Bopp Date: 2010-09-18T05:23:12+09:00 Subject: Re: ruby regexpresion On 9/17/2010 2:50 PM, Ruwan Budha wrote: > Jeremy thanks so much for your help......it works. > > could you please explain further how %r{(\(.*\)).*/(.*)'..(.*)} > > captures the 3 interested part from the string...... > > I know you have explained but if you could kindly explain more with an > easy way I think it will help me to learn.... > > thanks again Sure thing. Especially when you're learning about regular expressions it really helps to start by breaking them down. You often have to do that even when you're experienced but looking at someone else's work. ;-) Be aware that I'm going to gloss over quite a bit here, so you still need to learn some basics. We can break down this regexp into 5 parts: (\(.*\)) <- The first capture .*/ <- Noise (.*) <- The second capture '.. <- More noise (.*) <- The last capture Anything within the unescaped parenthesis will be captured and assigned to one of the $1, $2, etc. global variables. In the first capture, we need to capture the literal parenthesis which appear in the string, so we escape these with backslashes in order to remove their special meaning to the regular expression engine. Then we want to slurp up any text between the parenthesis, so we use ".*". The first bit of noise absorbs the uninteresting bits of the string which follow up to and including the forward slash. The * operator is greedy and dot (.) matches any single character, so .* will match everything including forward slashes. It only stops because we specifically said that this chunk of the regexp must end with a forward slash. Since it would be impossible to consume the last forward slash and still match one more, this noise accumulation stops. It's not within parenthesis, so it's not captured either. The second capture cooperates with the following noise section. The capture part would greedily consume everything much like the first noise section; however, the second noise section requires that a single quote be available to start it. There must be at least 1 single quote left in the string, so the second capture stops when there is only 1 left in this case. The second noise section goes on to consume 2 more characters after the single quote. This is because of the dot dot (..) in it. Each matches 1 of any single character. Finally the last capture section is left to consume the rest of the string exactly as the second capture would have done had nothing else followed it in the regexp. When building regexps, it's almost always a good idea to start small and build up. I like using irb for this since it gives me rapid results. You'll need more basics than I can provide here though if you want to become proficient. Good luck! -Jeremy