简体   繁体   中英

Check first Letter of Line in File Uppercase

How would I write a regular expression that checks the first letter on a line in a text file and then put something in front of the character if its uppercase? This is what I have so far:

import re

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

Regular expressions are not needed here, as you can solve the problem with the built-in isupper() function:

if word[0].isupper():
    new_s = "Something in front %s" % word

How would I write a regular expression that checks the first letter on a line:

>>> re.sub(r'^([A-Z])',r'>\1',"Abc")
'>Abc'
>>> re.sub(r'^([A-Z])',r'>\1',"abc")
'abc'

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