使用re.compile的正则表达式



我希望单词"a"在以下情况下大写

如果"a"在两个单个字符之间,或者后面跟着一个单个字符,并且是句子的结尾,或者后面跟一个数字或字母数字,那么它应该大写。我怎样才能做到这一点?

r a t             -->        r A t
It is r a t xxx   -->        It is r A t xxx
It is r a         -->        It is r A
It is r a(space)  -->        It is r a
Itisr a t         -->        Itisr a t
Itisr a txxx      -->        Itisr a txxx
It is r a txxx    -->        It is r a txxx

我使用了以下逻辑,但似乎在少数情况下不起作用。你能帮忙吗。

pattern = re.compile(r"^(.* [A-Za-z] )(a)( [A-Za-z] *.*)")
match = pattern.search(inputstring)  #//inputstring is above string
if match:
inputstring = f'{match.group(1)}{match.group(2).capitalize()}{match.group(3)}'
e = "It is r a txxx h  u ".split()
f = " ".join(e)
for i in range(len(e)):
if i == 0 or i == len(e) - 1:
continue
if len(e[i - 1]) == 1 and len(e[i + 1]) == 1 and len(e[i]) == 1:
if e[i - 1].islower() and e[i + 1].islower():
e[i] = e[i].upper()
f = " ".join(e)
print(f)

这就是你想要的?

在这种情况下,我将使用regex模块而不是Python的re,因为后者只支持固定宽度的外观机制。

import regex
s = """r a t
It is r a t xxx
It is r a
It is r a                   #'a' followed by a space
Itisr a t
Itisr a txxx
It is r a txxx"""
n = s.split('n')
r = regex.compile(r'((?<=bw(s)*b)a(?=b(s)*wb)|((?<=bw(s)*b)a$))')
for j in n:
if r.search(j) != None:
g = r.search(j).span()
print(''.join(z if y != g[0] else 'A' for y, z in enumerate(j)))
r A t
It is r A t xxx
It is r A

虽然这可能会解决问题,但需要谨慎。

最新更新