# Regexp question difference between ^ and \\A

**URL:** <https://www.ruby-forum.com/t/regexp-question-difference-between-and-a/175798>\
**Category:** Ruby\
**Created:** [September 20, 2009, 3:39am UTC](https://www.ruby-forum.com/t/regexp-question-difference-between-and-a/175798 "2009-09-20T03:39:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hunt\_J](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b9e5f3/32.png) [@Hunt\_J](https://www.ruby-forum.com/u/Hunt_J)\
**Post date:** [September 20, 2009, 3:39am UTC](https://www.ruby-forum.com/t/regexp-question-difference-between-and-a/175798/1 "2009-09-20T03:39:33Z")

</div>

Hi

I’m trying to find good use examples to show a difference  
between ^ and \A in Ruby regular expressions.

\A is for beginning of a string  
^ beginning of a line or string

As a regular expression runs one line by line (if I’m correct),  
I think both are the same.

I’m a little confused with when to use which.

Jon

---

<div class="post-metadata">

**Author:** ![Hunt\_J](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b9e5f3/32.png) [@Hunt\_J](https://www.ruby-forum.com/u/Hunt_J)\
**Post date:** [September 20, 2009, 6:08am UTC](https://www.ruby-forum.com/t/regexp-question-difference-between-and-a/175798/2 "2009-09-20T06:08:14Z")

</div>

On Sat, Sep 19, 2009 at 9:39 PM, Hunt J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Hi
> 
> I’m trying to find good use examples to show a difference  
> between ^ and \A in Ruby regular expressions.
> 
> \A is for beginning of a string  
> ^ beginning of a line or string
> 
> As a regular expression runs one line by line (if I’m correct),  
> I think both are the same.

No you are not correct, and they aren’t the same:

irb(main):001:0\> /\Axyzzy/ =~ “xyzzy”  
=\> 0  
irb(main):002:0\> /\Axyzzy/ =~ “abc\nxyzzy”  
=\> nil  
irb(main):003:0\> /^xyzzy/ =~ “xyzzy”  
=\> 0  
irb(main):004:0\> /^xyzzy/ =~ “abc\nxyzzy”  
=\> 4

–  
Rick DeNatale

Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)  
WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)

---

<div class="post-metadata">

**Author:** ![Hunt\_J](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b9e5f3/32.png) [@Hunt\_J](https://www.ruby-forum.com/u/Hunt_J)\
**Post date:** [September 20, 2009, 6:58am UTC](https://www.ruby-forum.com/t/regexp-question-difference-between-and-a/175798/3 "2009-09-20T06:58:47Z")

</div>

Hunt J. wrote:

> Hi
> 
> I’m trying to find good use examples to show a difference  
> between ^ and \A in Ruby regular expressions.
> 
> \A is for beginning of a string  
> ^ beginning of a line or string
> 
> As a regular expression runs one line by line (if I’m correct),  
> I think both are the same.

What does “runs one line by line” mean? A regular expression may  
examine the whole darn string–character by character. For example:

str = “abc\n123”  
results = str.scan(/./m)  
p results

–output:–  
[“a”, “b”, “c”, “\n”, “1”, “2”, “3”]

Do you consider that “runs one line by line”?

\A matches just before the _FIRST_ character of a string–and nowhere  
else:

str = “abc\n123”  
results = str.scan(/\A…/)  
p results

–output:–  
[“abc”]

^ matches just before the _first_ character of a string, _AND_ just  
after any newline:

str = “abc\n123”  
results = str.scan(/^…/)  
p results

–output:–  
[“abc”, “123”]

If your string doesn’t have any newlines, guess what?

str = “abc123”  
results = str.scan(/\A…/)  
p results

–output:–  
[“abc”]

str = “abc123”  
results = str.scan(/^…/)  
p results

–output:–  
[“abc”]

… \A and ^ are equivalent.

---

<div class="post-metadata">

**Author:** ![Hunt\_J](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b9e5f3/32.png) [@Hunt\_J](https://www.ruby-forum.com/u/Hunt_J)\
**Post date:** [September 20, 2009, 8:09am UTC](https://www.ruby-forum.com/t/regexp-question-difference-between-and-a/175798/4 "2009-09-20T08:09:24Z")

</div>

Thanks all. Now I understand how they work.

Thanks.
