Hick
Hick

Reputation: 36404

How to do this using regex in python.

A bit confused trying to do a string match that accepts this:

Lets say a string S = "Download" Here, S can be "Download" or "DOWNLOAD" or "DoWNload". Thus, any character in the string can be an uppercase or a lowercase. Its rather easy to write a regex match for all upper case or all lower case letters or even a mix of letters, but I found it difficult to write a regex match that follows a particular order, which here is "Download".

I hope I was lucid here.

Upvotes: 0

Views: 104

Answers (4)

Acorn
Acorn

Reputation: 50497

What's wrong with: S == "Download" ?

Your question isn't clear about whether you want to ignore case or not.

"I found it difficult to write a regex match that follows a particular order"

A particular order of capitalisation or just a particular order of letters?

If you want to ignore case, just convert to lower-case before comparing:

S.lower() == "download"

Upvotes: 1

Abhijit
Abhijit

Reputation: 63727

s="Download"
re.findall("^[A-Z][a-z]*$",s)
['Download']
s="DownloaD"
re.findall("^[A-Z][a-z]*$",s)
[]
s="download"
re.findall("^[A-Z][a-z]*$",s)
[]

If I understand your question correctly, you want to match a string with the First Cap followed by small case. In such a scenario, the above should work.

Upvotes: 1

rplnt
rplnt

Reputation: 2409

First, you can covert the string before the match

re.search(expr, s.lower())

If you want to ingore the case, you can use flag re.IGNORECASE

re.search(expr, s, re.IGNORECASE)

See other available flags here: http://docs.python.org/library/re.html#module-contents

Upvotes: 5

Sven Marnach
Sven Marnach

Reputation: 601599

To check if "download" appears in a string regardless of case, you don't even need a regular expression.

"download" in s.lower()

will also work fine.

Upvotes: 6

Related Questions