Bug with end of string characters in regex?

J

Justin Coyne

/\A\n\Z/m.match("\n\n") => #<MatchData>

shouldn't this expression return nil?


These both return nil.
/\A\n\Z/m.match("a\n")
/\A\n\Z/m.match("\na")


My understanding is that \A and \Z always match at the beginning/end of
the string irrespective of newlines.

-Justin
 
A

Aldric Giacomoni

Justin said:
/\A\n\Z/m.match("\n\n") => #<MatchData>

shouldn't this expression return nil?


These both return nil.
/\A\n\Z/m.match("a\n")
/\A\n\Z/m.match("\na")


My understanding is that \A and \Z always match at the beginning/end of
the string irrespective of newlines.

-Justin

Check out rubular.com

You are asking for:

\A # beginning of string
\n # newline
\Z # end of string

And by the way ... "\n" and /\n/ may not be the same, depending on what
the regexp wants to do with the backslash! you could try escaping it.
What do you want to match?
maybe:
/A # beginning of string
* # any characters
\\n # newline
* # any characters
/Z # end of string
 
J

Justin Coyne

Looks like I've solved my own problem the \Z in ruby doesn't have the
same semantics as it does in other languages. There is a \z which is
what I wanted so as expected:

/\A\n\z/m.match("\n\n") => nil


-Justin
 
J

Jesús Gabriel y Galán

Looks like I've solved my own problem the \Z in ruby doesn't have the
same semantics as it does in other languages. =A0There is a \z which is
what I wanted so as expected:

/\A\n\z/m.match("\n\n") =3D> nil

\z end of a string
\Z end of a string, or before newline at the end

So, \Z allows an extra \n at the end of the string.

Jesus.
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top