What is the meaning of \A and \Z

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.

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.

Oh boy. Welcome to “Regular Expressions”. Time for you to use Google!

Pro Tip: http://rubular.com/ will become your best friend.

On Wed, Apr 7, 2010 at 8:21 PM, Amishera Amishera
[email protected] 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.

Not exact, \z is the one that matches end of string. \Z still allows
some optional stuff.

That’s regular expressions.

On Wed, Apr 7, 2010 at 1:21 PM, Amishera Amishera
[email protected]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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs