简体   繁体   中英

Reg expression seems like it should be greedy, but acting non-greedy

I'm surprised by this:

dfnew = df.rename(columns=lambda x: re.sub('.*(\d?\d\.\d).*','ass \\1', x ))

If the column is called "Assignment 3.2" it is renamed to "ass 3.2" (good) But, if the column is called "Assignment 12.2" it is renamed to "ass 2.2" (not good)

I thought I had it set up just right. Can you see what I am missing?

尝试这个:

dfnew = df.rename(columns=lambda x: re.sub('.* (\d+\.\d).*','ass \\1', x ))

Thanks for your help. Here's the solution:

dfnew = df.rename(columns=lambda x: re.sub('.*?(\d?\d\.\d).*','ass \\1', x ))

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