使用正则表达式将python中的大写重复字母替换为单个小写字母 [英] Using a regular expression to replace upper case repeated letters in python with a single lowercase letter

查看:66
本文介绍了使用正则表达式将python中的大写重复字母替换为单个小写字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将字符串中重复两次的大写字母的任何实例替换为该字母的单个小写实例.我正在使用以下正则表达式,它能够匹配重复的大写字母,但我不确定如何使被替换的字母变为小写.

I am trying to replace any instances of uppercase letters that repeat themselves twice in a string with a single instance of that letter in a lower case. I am using the following regular expression and it is able to match the repeated upper case letters, but I am unsure as how to make the letter that is being replaced lower case.

import re
s = 'start TT end'
re.sub(r'([A-Z]){2}', r"\1", s)
>>> 'start T end'

如何使\1"小写?我不应该使用正则表达式来执行此操作吗?

How can I make the "\1" lower case? Should I not be using a regular expression to do this?

推荐答案

传递一个函数 作为 repl 参数.MatchObject 被传递给这个函数并且.group(1) 给出第一个带括号的子组:

Pass a function as the repl argument. The MatchObject is passed to this function and .group(1) gives the first parenthesized subgroup:

import re
s = 'start TT end'
callback = lambda pat: pat.group(1).lower()
re.sub(r'([A-Z]){2}', callback, s)

编辑
是的,您应该使用 ([A-Z])\1 而不是 ([A-Z]){2} 以便匹配,例如AZ.(见@bobince 的 答案.)

EDIT
And yes, you should use ([A-Z])\1 instead of ([A-Z]){2} in order to not match e.g. AZ. (See @bobince's answer.)

import re
s = 'start TT end'
re.sub(r'([A-Z])\1', lambda pat: pat.group(1).lower(), s) # Inline

给出:

'start t end'

这篇关于使用正则表达式将python中的大写重复字母替换为单个小写字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆