Tg.
Tg.

Reputation: 5794

regular expression: chopping last character if second last is upper case?

I want to chop off last character if it is lower case and second last one is upper case. For example.

14-ME -> 14-ME
MEA  -> MEA
MEAm -> MEA  #like this one
mama -> mama

How to write the regx ? I am thinking of something like r"(.+?)" but not sure how to do conditional thing on the last part. The word could have anything like ()-,+ too.

thanks

Upvotes: 1

Views: 1470

Answers (4)

Justin Peel
Justin Peel

Reputation: 47082

Here's how I'd do it with a regexp.

strings = ["14-ME","MEA","MEAm","mama"]
p = re.compile(r"([A-Z])[a-z]$")
for s in strings:
    print p.sub(r"\1", s)

which gives

14-ME
MEA
MEA
mama

It wasn't clear to me if you wanted it to match the end of the string, but that's what my regexp does.

Upvotes: 1

mathematical.coffee
mathematical.coffee

Reputation: 56935

Try r"(.+[A-Z])[a-z]\b".

import re
regex = r"(.+[A-Z])[a-z]\b"

re.match(regex,'14-ME') # None
re.match(regex,'MEA') # None
re.match(regex,'MEAm') # <_sre.SRE_Match object at 0x.. >
re.match(regex,'mama') # None

For those objects that match you can grab out all but the last character using .group:

a = re.match(regex,'MEAm')
a.group(1) # 'MEA'

Upvotes: 1

wkl
wkl

Reputation: 79991

Don't really need a regex to do this when you can write a simple piece of code to do it.

def chop_char(some_string):
    try:
        # determine if the second to last character is upper case
        if some_string[-2].istitle() and not some_string[-1].istitle():
            return some_string[:-1] # slice off the last character
    except IndexError:
        # string isn't long enough to have a 2nd to last char (i.e. it's only 1 character)
        pass
    return some_string

Or if you don't want the exception stuff...

def chop_char(s):
    if len(s) > 1:
        if s[-2].istitle() and not s[-1].istitle():
            return s[:-1]
    return s

Upvotes: 2

Daniel
Daniel

Reputation: 1087

I know nothing about python regex (or any regex really), but you probably want some thing to match like: [list of uppercase letters][list of lowercase letter][end of word]

Upvotes: 0

Related Questions