1

我将如何编写一个正则表达式来检查文本文件中一行的第一个字母,然后在字符前面放置一些东西(如果它是大写的)?这是我到目前为止所拥有的:

import re

p = re.compile(r'(.*)([A-Z])(.*)>')
...
pr = p.sub(r'\1<P>\2</p>', line)
4

2 回答 2

12

这里不需要正则表达式,因为您可以使用内置的isupper()函数解决问题:

if word[0].isupper():
    new_s = "Something in front %s" % word
于 2013-07-08T13:09:30.437 回答
3

我将如何编写一个正则表达式来检查一行的第一个字母:

>>> re.sub(r'^([A-Z])',r'>\1',"Abc")
'>Abc'
>>> re.sub(r'^([A-Z])',r'>\1',"abc")
'abc'
于 2013-07-08T13:09:24.293 回答