user3078335
user3078335

Reputation: 781

Get specific words in a string

I am fairly new to python. I have a this string

"DEALER: 'S up, Bubbless?
BUBBLES: Hey.
DEALER: Well, there you go.
JUNKIE: Well, what you got?
DEALER: I got some starters. "

I am trying to get all the words in uppercase that end with a colon. For example, I get DEALER, BUBBLES and JUNKIE from the string above. Thanks

This is what i tried. Seems to work. But not as accurate as I would want.

s = "DEALER: 'S up, Bubbless? BUBBLES: Hey. DEALER: Well, there you go. JUNKIE: Well, what you got?DEALER: I got some starters.";
#print l
print [ t for t in s.split() if t.endswith(':') ]

Upvotes: 0

Views: 77

Answers (2)

Graeme Stuart
Graeme Stuart

Reputation: 6063

You'll need to get rid of repeats. A nice way is with a set.

import re

mystring = """
DEALER: 'S up, Bubbless?
BUBBLES: Hey.
DEALER: Well, there you go.
JUNKIE: Well, what you got?
DEALER: I got some starters. """

p = re.compile('([A-Z]*):')
s = set(p.findall(mystring))

print s

This results in a unique set of names

set(['JUNKIE', 'DEALER', 'BUBBLES'])

Upvotes: 2

corn3lius
corn3lius

Reputation: 5005

import re 

regex = re.compile( "(?P<name>[A-Z]*:)[\s\w]*" ) 

actors = regex.findall(text)

Upvotes: 1

Related Questions