From: Josh Cheek Date: 2010-04-08T03:37:09+09:00 Subject: Re: what is the meaning of \A and \Z --000e0cd112c07613100483a9d9c3 Content-Type: text/plain; charset=ISO-8859-1 On Wed, Apr 7, 2010 at 1:21 PM, Amishera Amishera wrote: > It is said that \A matches the beginning of the string and \Z matches > the end of the string. I am not sure what means. > -- > Posted via http://www.ruby-forum.com/. > > "xyz\nuvw".scan(/\Ax/) # => ["x"] "uvw\nxyz".scan(/\Ax/) # => [] We are looking for the beginning of the string, followed by the character x. The string here has 2 lines, when the first line (the beginning of the string) begins with x, it matches. When the second line begins with x and the first doesn't, it does not match. ----- "uvw\nxyz".scan(/^x/)# => ["x"] "xyz\nuvw".scan(/^x/)# => ["x"] This time, we use the caret, which matches the beginning of the line, it matches in both cases ----- "uvw\nxyz".scan(/\A./) # => ["u"] "uvw\nxyz".scan(/^./)# => ["u", "x"] Now we use a dot, which will match any character. Basically telling it "find the beginning of the string followed by some character" and "find the beginning of the line followed by some character". In the first case, it matches the "u" because that is at the beginning of the string. In the second, it matches "u" and "x" because they are at the beginning of the lines. --000e0cd112c07613100483a9d9c3--