# Regular expressions: Find part of a string

**URL:** <https://www.ruby-forum.com/t/regular-expressions-find-part-of-a-string/169530>\
**Category:** Ruby\
**Created:** [June 4, 2009, 10:06pm UTC](https://www.ruby-forum.com/t/regular-expressions-find-part-of-a-string/169530 "2009-06-04T22:06:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jermaine](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Jermaine](https://www.ruby-forum.com/u/Jermaine)\
**Post date:** [June 4, 2009, 10:06pm UTC](https://www.ruby-forum.com/t/regular-expressions-find-part-of-a-string/169530/1 "2009-06-04T22:06:16Z")

</div>

Hello Everyone,

I’m quite new to regular expressions, and I’m looking for a way to  
find the first 3 letters in a string.

So let’s say I have the following string: “foobar”, I want to know  
what the first three letters of this string is (in this case it’s  
foo).

What is the regex to make this happen?

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![Jermaine](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Jermaine](https://www.ruby-forum.com/u/Jermaine)\
**Post date:** [June 4, 2009, 10:15pm UTC](https://www.ruby-forum.com/t/regular-expressions-find-part-of-a-string/169530/2 "2009-06-04T22:15:05Z")

</div>

Jermaine wrote:

> Thanks in advance.

p “foobar”[/…/]

# simplest

p “foobar”[/\A…/]

# \A anchors to beginning of string, which is

# same as above for this regex

p “foobar”[/.{3,3}/]

# accepts between 3 and 3 chars

---

<div class="post-metadata">

**Author:** ![Jermaine](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Jermaine](https://www.ruby-forum.com/u/Jermaine)\
**Post date:** [June 4, 2009, 10:17pm UTC](https://www.ruby-forum.com/t/regular-expressions-find-part-of-a-string/169530/3 "2009-06-04T22:17:26Z")

</div>

You could use regex for this, but if you just want the first three  
characters, why not use the substring?

s=“foobar”  
s[0,3]

Using regex, you could do something like:  
“foobar”.match(/^…/).to\_s

Matt

–  
“… if you do follow your bliss you put yourself on a kind of  
track that has been there all the while, waiting for you, and the life  
that you ought to be living is the one you are living. When you can  
see that, you begin to meet people who are in your field of bliss, and  
they open doors to you. I say, follow your bliss and don’t be afraid,  
and doors will open where you didn’t know they were going to be.” –  
Joseph Campbell

---

<div class="post-metadata">

**Author:** ![Jermaine](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Jermaine](https://www.ruby-forum.com/u/Jermaine)\
**Post date:** [June 4, 2009, 10:30pm UTC](https://www.ruby-forum.com/t/regular-expressions-find-part-of-a-string/169530/4 "2009-06-04T22:30:59Z")

</div>

On Jun 4, 10:16 pm, “Matthew K. Williams” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Hello Everyone,  
> > Thanks in advance.  
> > Great stuff. Very simple and concise, worked out great for me.  
> > Thanks guys!

---

<div class="post-metadata">

**Author:** ![Jermaine](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Jermaine](https://www.ruby-forum.com/u/Jermaine)\
**Post date:** [June 5, 2009, 12:10am UTC](https://www.ruby-forum.com/t/regular-expressions-find-part-of-a-string/169530/5 "2009-06-05T00:10:44Z")

</div>

In addition to other comments, you can go here and test your regular  
expressions:

> **[Rubular](https://rubular.com/)**
>
> Ruby-based regular expression editor/tester
