Regular Expression for a string

T

Teja

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
 
J

James Stroud

Teja said:
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/
 
R

Roy Smith

"Teja said:
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.
 
T

Teja

James said:
Teja said:
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
 
T

Teja

Roy said:
Teja said:
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
 
J

John Machin

Teja said:
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
 
T

Teja

John said:
Teja said:
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....:)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top