Forum: Ruby Regular expressions: Find part of a string

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
916771842b2bdd0d28e4caa6b38bcb38?d=identicon&s=25 Jermaine (Guest)
on 2009-06-04 22:06
(Received via mailing list)
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

What is the regex to make this happen?

Thanks in advance.
47b1910084592eb77a032bc7d8d1a84e?d=identicon&s=25 Joel VanderWerf (Guest)
on 2009-06-04 22:15
(Received via mailing list)
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
C3a0d479bd322818d766a7e869c6c5b2?d=identicon&s=25 Matthew K. Williams (Guest)
on 2009-06-04 22:17
(Received via mailing list)
You could use regex for this, but if you just want the first three
characters, why not use the substring?


Using regex, you could do something like:


"... 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
916771842b2bdd0d28e4caa6b38bcb38?d=identicon&s=25 Jermaine (Guest)
on 2009-06-04 22:30
(Received via mailing list)
On Jun 4, 10:16 pm, "Matthew K. Williams" <> wrote:
> > Hello Everyone,
> > Thanks in advance.
Great stuff. Very simple and concise, worked out great for me.
Thanks guys!
40ba541bc54ef6e3c9e6d9e101e19e26?d=identicon&s=25 Rilindo Foster (Guest)
on 2009-06-05 00:10
(Received via mailing list)
In addition to other comments, you can go here and test your regular
This topic is locked and can not be replied to.