what is the meaning of \A and \Z

  • Thread starter Amishera Amishera
  • Start date
A

Amishera Amishera

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.
 
A

Aldric Giacomoni

Amishera said:
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.
 
X

Xavier Noria

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.
 
J

Josh Cheek

[Note: parts of this message were removed to make it a legal post.]

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.
"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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,158
Messages
2,570,882
Members
47,414
Latest member
djangoframe

Latest Threads

Top