Home > Software design >  editing the first word in match, regex
editing the first word in match, regex

Time:08-13

I have a regex problem. In found matches (these are lines from a text file) I want to edit them so I can get shortened version of these lines. For example:

matches=[Dog Alex, Dog Chriss, Cat Susan, Lizard Bob, and so on]

From this I want to get:

new_version=[D.Alex, D.Chriss, C.Susan, and so on]

So I wrote this code:

for match in matches:
    mpattern=re.compile(r'^\w (. )')
    match=re.sub(mpattern, r'[A-Z]\. (. )', match)
    list_of_descriptions.append(match)

but it doesn't work correctly :(

I need the program to find the first word (which could begin with any letter) and then shorten it to the first letter and add a dot. Could someone help me, please? I'm using Python 3.7.9.

CodePudding user response:

The regex should capture the first letter, but should allow for more letters in the first word. On the other hand, as you're not planning to change anything to the second word, there is no need to include it in the match.

Side note: when you compile your regex, you can call the sub method on that object, instead of on re.

So:

mpattern = re.compile(r'^(\w)\w* ')
list_of_descriptions = [mpattern.sub(r'\1.', match) for match in matches]
  • Related