Regular expressions: Find part of a string

Discussion in 'Ruby' started by Jermaine, Jun 4, 2009.

  1. Jermaine

    Jermaine Guest

    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.
     
    Jermaine, Jun 4, 2009
    #1
    1. Advertising

  2. Jermaine wrote:
    > 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.


    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


    --
    vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
     
    Joel VanderWerf, Jun 4, 2009
    #2
    1. Advertising

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


    On Fri, 5 Jun 2009, Jermaine wrote:

    > 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.
    >
    >
     
    Matthew K. Williams, Jun 4, 2009
    #3
  4. Jermaine

    Jermaine Guest

    On Jun 4, 10:16 pm, "Matthew K. Williams" <> wrote:
    > 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
    >
    > On Fri, 5 Jun 2009, Jermaine wrote:
    > > 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.


    Great stuff. Very simple and concise, worked out great for me.
    Thanks guys!
     
    Jermaine, Jun 4, 2009
    #4
  5. In addition to other comments, you can go here and test your regular
    expressions:

    http://rubular.com/

    On Jun 4, 2009, at 4:05 PM, Jermaine wrote:

    > 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.
    >
     
    Rilindo Foster, Jun 4, 2009
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jay Douglas
    Replies:
    0
    Views:
    644
    Jay Douglas
    Aug 15, 2003
  2. Mark
    Replies:
    0
    Views:
    313
  3. Replies:
    6
    Views:
    365
    dusans
    Aug 1, 2008
  4. SROSeaner

    Regular Expressions to find URL's in text

    SROSeaner, Oct 8, 2004, in forum: ASP General
    Replies:
    3
    Views:
    143
    SROSeaner
    Oct 11, 2004
  5. Noman Shapiro
    Replies:
    0
    Views:
    262
    Noman Shapiro
    Jul 17, 2013
Loading...

Share This Page