简体   繁体   中英

editing the first word in match, regex

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.

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM