# Bug with end of string characters in regex?

**URL:** <https://www.ruby-forum.com/t/bug-with-end-of-string-characters-in-regex/181553>\
**Category:** Ruby\
**Created:** [January 13, 2010, 6:25pm UTC](https://www.ruby-forum.com/t/bug-with-end-of-string-characters-in-regex/181553 "2010-01-13T18:25:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![justincollins](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b5ac83/32.png) [@justincollins](https://www.ruby-forum.com/u/justincollins)\
**Post date:** [January 13, 2010, 6:25pm UTC](https://www.ruby-forum.com/t/bug-with-end-of-string-characters-in-regex/181553/1 "2010-01-13T18:25:08Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![justincollins](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b5ac83/32.png) [@justincollins](https://www.ruby-forum.com/u/justincollins)\
**Post date:** [January 13, 2010, 6:32pm UTC](https://www.ruby-forum.com/t/bug-with-end-of-string-characters-in-regex/181553/2 "2010-01-13T18:32:28Z")

</div>

Justin C. wrote:

> /\A\n\Z/m.match(“\n\n”) =\> #
> 
> 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](http://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

---

<div class="post-metadata">

**Author:** ![justincollins](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b5ac83/32.png) [@justincollins](https://www.ruby-forum.com/u/justincollins)\
**Post date:** [January 13, 2010, 6:37pm UTC](https://www.ruby-forum.com/t/bug-with-end-of-string-characters-in-regex/181553/3 "2010-01-13T18:37:31Z")

</div>

On Wed, Jan 13, 2010 at 6:33 PM, Justin C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> 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

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

---

<div class="post-metadata">

**Author:** ![justincollins](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b5ac83/32.png) [@justincollins](https://www.ruby-forum.com/u/justincollins)\
**Post date:** [January 13, 2010, 6:33pm UTC](https://www.ruby-forum.com/t/bug-with-end-of-string-characters-in-regex/181553/4 "2010-01-13T18:33:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![justincollins](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b5ac83/32.png) [@justincollins](https://www.ruby-forum.com/u/justincollins)\
**Post date:** [January 14, 2010, 12:50pm UTC](https://www.ruby-forum.com/t/bug-with-end-of-string-characters-in-regex/181553/5 "2010-01-14T12:50:54Z")

</div>

2010/1/13 Jesús Gabriel y Galán [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> So, \Z allows an extra \n at the end of the string.  
> For the reference here’s the doc:  
> [サービス終了のお知らせ](http://www.geocities.jp/kosako3/oniguruma/doc/RE.txt)

Cheers

robert

---

<div class="post-metadata">

**Author:** ![justincollins](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b5ac83/32.png) [@justincollins](https://www.ruby-forum.com/u/justincollins)\
**Post date:** [January 14, 2010, 1:43pm UTC](https://www.ruby-forum.com/t/bug-with-end-of-string-characters-in-regex/181553/6 "2010-01-14T13:43:00Z")

</div>

On Thu, Jan 14, 2010 at 12:50 PM, Robert K.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > So, \Z allows an extra \n at the end of the string.
> 
> For the reference here’s the doc:  
> [サービス終了のお知らせ](http://www.geocities.jp/kosako3/oniguruma/doc/RE.txt)

Good to know thanks. I copied the quote from:

> **[Ruby | zenspider.com | by ryan davis](https://www.zenspider.com/ruby/)**
>
> Ruby facts, opinions, source code, links, and stuff.

Thanks.

Jesus.
