WebOrCode
WebOrCode

Reputation: 7294

Regular expression where first letter in a word is uppercase, and word is surrounded by _

Basically I wont regular expression that will accept only this:

Dog  
Cat_Dog   
Cat_Dog_Mouse  

Numbers are allowed. [0-9] And are treated as words

Dog_003  -> OK  
Dog003   -> NOT OK  
Dog_003_ -> NOT OK  

This is not allowed:

DoG  
DOg  
DOG (only first letter is uppercase)
Cat_Dog_
_Cat_Dog_ (_ can be only between words) 

It will be used in Python.

My solution

reTitle2 = re.compile(
            """
             ^([A-Z]{1}|[A-Z]{1}[a-z]*|[0-9]+)         # first part 
              (_[A-Z]{1}|_[A-Z]{1}[a-z]*|_[0-9]+)*     # zero or more times 
              ([A-Z]{1}|[A-Z]{1}[a-z]*|[0-9]+){0,1}$   # last part
            """
            , re.UNICODE | re.VERBOSE)

There are some redundant parts, but I make it more easy to understand.

Upvotes: 0

Views: 77

Answers (2)

WebOrCode
WebOrCode

Reputation: 7294

My solution

reTitle2 = re.compile(
            """
             ^([A-Z]{1}|[A-Z]{1}[a-z]*|[0-9]+)         # first part 
              (_[A-Z]{1}|_[A-Z]{1}[a-z]*|_[0-9]+)*     # zero or more times 
              ([A-Z]{1}|[A-Z]{1}[a-z]*|[0-9]+){0,1}$   # last part
            """
            , re.UNICODE | re.VERBOSE)

There are some redundant parts, but I make it more easy to understand.

Upvotes: 1

Paul Evans
Paul Evans

Reputation: 27577

The regex is:

[A-Z][a-z]*(_([A-Z][a-z]*|[0-9]+))*

or, if 003_Dog is allowed:

([A-Z][a-z]*|[0-9]+)(_([A-Z][a-z]*|[0-9]+))*

Upvotes: 3

Related Questions