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):
        query=self.search.get().capitalize()
        if query in ab.book:
            Interface.result=query.capitalize(),str(ab.book[query])
        else:
            Interface.result='Query not found.'
        show()
'''-------------------------------------------'''
def show():
    showing=Toplevel()
    showing.title('Contact')
    showing.geometry('225x110')
    Display_contact(showing)

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

    def create_widget(self):
        self.contact=Label(self,text=''.join(Interface.result))
        self.contact.grid(row=1, column=3, columnspan=3)
        self.spacer=Label(self,text='   ')
        self.spacer.grid(row=0,column=0,rowspan=4,columnspan=3)

I tried creating a TopLevel with radio buttons for

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

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, networking, learning, and sharing knowledge.