# What is the meaning of \\A and \\Z

**URL:** <https://www.ruby-forum.com/t/what-is-the-meaning-of-a-and-z/186485>\
**Category:** Ruby\
**Created:** [April 7, 2010, 8:21pm UTC](https://www.ruby-forum.com/t/what-is-the-meaning-of-a-and-z/186485 "2010-04-07T20:21:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![amishera](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/59ef9b/32.png) [@amishera](https://www.ruby-forum.com/u/amishera)\
**Post date:** [April 7, 2010, 8:21pm UTC](https://www.ruby-forum.com/t/what-is-the-meaning-of-a-and-z/186485/1 "2010-04-07T20:21:01Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![amishera](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/59ef9b/32.png) [@amishera](https://www.ruby-forum.com/u/amishera)\
**Post date:** [April 7, 2010, 8:22pm UTC](https://www.ruby-forum.com/t/what-is-the-meaning-of-a-and-z/186485/2 "2010-04-07T20:22:14Z")

</div>

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/](http://rubular.com/) will become your best friend.

---

<div class="post-metadata">

**Author:** ![amishera](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/59ef9b/32.png) [@amishera](https://www.ruby-forum.com/u/amishera)\
**Post date:** [April 7, 2010, 8:29pm UTC](https://www.ruby-forum.com/t/what-is-the-meaning-of-a-and-z/186485/3 "2010-04-07T20:29:26Z")

</div>

On Wed, Apr 7, 2010 at 8:21 PM, Amishera Amishera  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![amishera](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/59ef9b/32.png) [@amishera](https://www.ruby-forum.com/u/amishera)\
**Post date:** [April 7, 2010, 8:37pm UTC](https://www.ruby-forum.com/t/what-is-the-meaning-of-a-and-z/186485/4 "2010-04-07T20:37:38Z")

</div>

On Wed, Apr 7, 2010 at 1:21 PM, Amishera Amishera  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)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/](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.
