Search code examples
pythonregexwords

finding two or more consecutive words starts with upper and replacing with their abbreviation (regex)


i want to replace two or more consecutive words that starts with an upper character and replace them with their abbreviation, i managed to find the words with

def find(name):
        return re.findall('([A-Z][a-z]+(?=\s[A-Z])(?:\s[A-Z][a-z]+)+)', name)

but when i try to replace the words i just couldn't manage to do it

here what i got

import re


def main():
    name = raw_input(" Enter name: ")

    print find(name)


def find(name):
    return re.sub(r'([A-Z][a-z]+(?=\s[A-Z])(?:\s[A-Z][a-z]+)+)', replacement, name)


def replacement(match):
    return match.group[0].upper()

main()

for example

input: I went to the Annual General Meeting. Output: I went to the AGM.

appreciate any help


Solution

  • If you modify your replacement function as follows, your example should be in working order:

    def replacement(match):
        return ''.join(y[0] for y in m.group(0).split())