From: Kc Co Date: 2010-08-01T13:14:31+09:00 Subject: Re: Problem in 2 code Amir Ebrahimifard wrote: > Hi > I dont understand results of these 2 code ? > please explain me what do every code do ? > > 1 - > x = "This is a test".match( /(\w+)(\w+)/ ) > puts x[0] > puts x[1] > puts x[2] > > 2 - > x = "This is a test".match( /(\w+) (\w+)/ ) > puts x[0] > puts x[1] > puts x[2] /(\w+)(\w+)/ is a Regexp. If you want to know about it, you should probably look it up to help remove the confusion. \w+ means one or more word characters, so /(\w+)(\w+)/ means 2 word characters or more. Based on the string, the match method returns the first instance that matches it, which is "This". As to what the x[1] and x[2] put to the screen, I could be totally wrong about it but I'm guessing they might be the parts that match the parts of the Regexp given. However, it'd probably be best to ask someone else about that. In the second code, it's the same thing except there's a space between the two \w+. This means at least one word character followed by a space followed by at least one word character. That is why it returns the match "This is" instead of just "This". I hope this was helpful. -- Posted via http://www.ruby-forum.com/.