Regular Expression for a string

Discussion in 'Python' started by Teja, Nov 2, 2006.

  1. Teja

    Teja Guest

    HI all,

    I need to write a regular experssion for a string which satisfies the
    following a criteria :

    1) it should start with an alphabet
    2) it can contain alphabets/digits/_ from second character
    3) it can contain "[a-z]" or "[0-9]" at the end. but this is optional

    can any one please help me out.... pls....


    Teja.P
     
    Teja, Nov 2, 2006
    #1
    1. Advertising

  2. Teja

    James Stroud Guest

    Teja wrote:
    > HI all,
    >
    > I need to write a regular experssion for a string which satisfies the
    > following a criteria :
    >
    > 1) it should start with an alphabet
    > 2) it can contain alphabets/digits/_ from second character
    > 3) it can contain "[a-z]" or "[0-9]" at the end. but this is optional
    >
    > can any one please help me out.... pls....
    >
    >
    > Teja.P
    >


    This is as general as could be constructed given your vague requirements:

    "[a-zA-Z][a-zA-Z0-9_]+"

    You may need to provide examples.

    James



    --
    James Stroud
    UCLA-DOE Institute for Genomics and Proteomics
    Box 951570
    Los Angeles, CA 90095

    http://www.jamesstroud.com/
     
    James Stroud, Nov 2, 2006
    #2
    1. Advertising

  3. Teja

    Roy Smith Guest

    In article <>,
    "Teja" <> wrote:

    > HI all,
    >
    > I need to write a regular experssion for a string which satisfies the
    > following a criteria :
    >
    > 1) it should start with an alphabet


    I assume you mean, "It should start with a letter"?

    > 2) it can contain alphabets/digits/_ from second character
    > 3) it can contain "[a-z]" or "[0-9]" at the end. but this is optional


    How about: ^[a-zA-Z][a-zA-Z0-9_]*[a-z0-9]?$

    Probably many variations on that theme will also work.
     
    Roy Smith, Nov 2, 2006
    #3
  4. Teja

    Teja Guest

    James Stroud wrote:

    > Teja wrote:
    > > HI all,
    > >
    > > I need to write a regular experssion for a string which satisfies the
    > > following a criteria :
    > >
    > > 1) it should start with an alphabet
    > > 2) it can contain alphabets/digits/_ from second character
    > > 3) it can contain "[a-z]" or "[0-9]" at the end. but this is optional
    > >
    > > can any one please help me out.... pls....
    > >
    > >
    > > Teja.P
    > >

    >
    > This is as general as could be constructed given your vague requirements:
    >
    > "[a-zA-Z][a-zA-Z0-9_]+"
    >
    > You may need to provide examples.
    >
    > James
    >
    >
    >
    > --
    > James Stroud
    > UCLA-DOE Institute for Genomics and Proteomics
    > Box 951570
    > Los Angeles, CA 90095
    >
    > http://www.jamesstroud.com/


    Thnks James for ur quick reply.....
    Here is what I want actually

    Some examples

    string1
    string_1
    string1[0]
    string_1_str[a]
    STRING_1_[2]
    STRING_1_str[a][1][2]

    etc
    etc

    I need a regular expression for such strings
     
    Teja, Nov 2, 2006
    #4
  5. Teja

    Teja Guest

    Roy Smith wrote:

    > In article <>,
    > "Teja" <> wrote:
    >
    > > HI all,
    > >
    > > I need to write a regular experssion for a string which satisfies the
    > > following a criteria :
    > >
    > > 1) it should start with an alphabet

    >
    > I assume you mean, "It should start with a letter"?
    >
    > > 2) it can contain alphabets/digits/_ from second character
    > > 3) it can contain "[a-z]" or "[0-9]" at the end. but this is optional

    >
    > How about: ^[a-zA-Z][a-zA-Z0-9_]*[a-z0-9]?$
    >
    > Probably many variations on that theme will also work.


    Thnks Roy for ur quick reply.....
    Here is what I want actually

    Some examples

    string1
    string_1
    string1[0]
    string_1_str[a]
    STRING_1_[2]
    STRING_1_str[a][1][2]

    etc
    etc

    I need a regular expression for such strings
     
    Teja, Nov 2, 2006
    #5
  6. Teja

    John Machin Guest

    Teja wrote:
    > HI all,
    >
    > I need to write a regular experssion for a string which satisfies the
    > following a criteria :
    >
    > 1) it should start with an alphabet
    > 2) it can contain alphabets/digits/_ from second character
    > 3) it can contain "[a-z]" or "[0-9]" at the end. but this is optional


    Based on the examples you give later, you don't mean "optional" (0 or 1
    occurrences), you mean 0 or more occurrences.
    Assuming "alphabet" means "English alphabetic":

    r"[a-zA-Z][a-zA-Z0-9_]*(\[[a-z0-9]\])*"

    HTH,
    John
     
    John Machin, Nov 2, 2006
    #6
  7. Teja

    Teja Guest

    John Machin wrote:

    > Teja wrote:
    > > HI all,
    > >
    > > I need to write a regular experssion for a string which satisfies the
    > > following a criteria :
    > >
    > > 1) it should start with an alphabet
    > > 2) it can contain alphabets/digits/_ from second character
    > > 3) it can contain "[a-z]" or "[0-9]" at the end. but this is optional

    >
    > Based on the examples you give later, you don't mean "optional" (0 or 1
    > occurrences), you mean 0 or more occurrences.
    > Assuming "alphabet" means "English alphabetic":
    >
    > r"[a-zA-Z][a-zA-Z0-9_]*(\[[a-z0-9]\])*"
    >
    > HTH,
    > John


    Thnks a lot....:)
     
    Teja, Nov 2, 2006
    #7
    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. lucky
    Replies:
    11
    Views:
    2,566
    Lucky
    Nov 11, 2005
  2. Michael Kiermaier

    constant string in regular expression

    Michael Kiermaier, Jul 12, 2003, in forum: Perl
    Replies:
    2
    Views:
    5,022
    Eric J. Roode
    Jul 13, 2003
  3. VSK
    Replies:
    2
    Views:
    2,307
  4. =?iso-8859-1?B?bW9vcJk=?=

    Matching abitrary expression in a regular expression

    =?iso-8859-1?B?bW9vcJk=?=, Dec 1, 2005, in forum: Java
    Replies:
    8
    Views:
    851
    Alan Moore
    Dec 2, 2005
  5. GIMME
    Replies:
    3
    Views:
    11,974
    vforvikash
    Dec 29, 2008
Loading...

Share This Page