Convert letters to lower case

Question:

I use the following with respect to letters from any language:

text = regex.sub("[^p{alpha}d]+"," ",text

Can I use p{alpha} to convert letters to their lower case equivalent if such an equivalency exists? How would this regex look?

Asked By: Baz

||

Answers:

I believe you can find your answer here: http://docs.python.org/library/re.html#re.sub

You can provide a tolower function that takes a match object to the sub method which will return replacement string

Answered By: minus

You can change the re.findall("([A-Z]+)", text) to use whatever regex you need. This will just go through the matches, and replace each match with its lowercase equivalent:

text = 'ABCDEF_ghjiklm_OPQRSTUVWXYZ'
for f in re.findall("([A-Z]+)", text):
    text = text.replace(f, f.lower())
print text

Output is:

abcdef_ghjiklm_opqrstuvwxyz
Answered By: chown
>>> re.sub('[AEIOU]+', lambda m: m.group(0).lower(), 'SOME TEXT HERE')
'SoMe TeXT HeRe'
Answered By: Steven Rumbalski

As oxtopus suggested, you can simply convert letters to their lowercase version with text.lower() (no need for a regular expression). This works with Unicode strings too (À -> à, etc.)

Answered By: Eric O Lebigot

in languages like Perl or Js the regex engine supports L — python is poor that way

Answered By: ekkis
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.