I'm having a terrible time with regular expressions, possibly because I'm using a python2 reference, but maybe not. For example how would I find \s\w within a string and then change that character?

Recommended Answers

All 6 Replies

Do you mean searching for first letter of word:

re.findall('\s\w', r"For example how would I find \s\w within a string and then change that character?")

I meant how would I find a space and then a letter, and the str would be a variable, but I guess that doesn't really matter, that's just abstraction, and then how would I change that letter. Like if we had a two word string SA='Secret agent' and I wanted to .capitalize() agent.

You would use .title() ;)

Not so special, maybe but this leaves all but last word uncapitalized.

sa ='Secret agent'
m = re.match(r'(.*\s)(\w*)', sa)
>>> m.groups()
('Secret ', 'agent')
>>> ''.join(word.capitalize() for word in m.groups())
'Secret Agent'
>>> sa ='Secret agent office'
>>> m.groups()
('Secret ', 'agent')
>>> m = re.match(r'(.*\s)(\w*)', sa)
>>> m.groups()
('Secret agent ', 'office')
>>> ''.join(word.capitalize() for word in m.groups())
'Secret agent Office'

awesome, thanks. So I have a "not really" related question. I just hate flooding the board. I'm still working on the address book. If the user were to have more than one contact of the same name and they were to search for them, how would I make it possible for them to select which one they wanted? I'll show you the relevant code I have so far.

def searchfor(self):
        if query in ab.book:
            Interface.result='Query not found.'
def show():

class Display_contact(Frame):
    '''Show a contact'''
    def __init__(self,master):
        super(Display_contact, self).__init__(master)

    def create_widget(self):
        self.contact.grid(row=1, column=3, columnspan=3)
        self.spacer=Label(self,text='   ')

I tried creating a TopLevel with radio buttons for

for keys in ab.book.keys()
   if keys==query:
if len(somelist)>1:

obviously that's not verbatim

I'm just going to start a new thread because this one could be very useful.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.