0

I'm trying to simply get everything after the colon in the following:

hfarnsworth:204b319de6f41bbfdbcb28da724dda23

And then everything before the space in the following:

29ca0a80180e9346295920344d64d1ce ::: 25basement

Here's what I have:

for line in f: 
    line = line.rstrip() #to remove \n
    line = re.compile('.* ',line) #everything before space. 
    print line

Any tips to point me in the corrent direction? Thanks!

Also, is re.compile the correct function to use if I want the matched string returned? I'm pretty new at python too. Thanks!!

3 Answers 3

4
string = "hfarnsworth:204b319de6f41bbfdbcb28da724dda23"
print(string.split(":")[1:])
string = "29ca0a80180e9346295920344d64d1ce ::: 25basement"
print(string.split(" ")[0])
Sign up to request clarification or add additional context in comments.

Comments

3

At first you should probably take a careful look at the doc for re.compile. It doesn't expect for second parameter to be a string to lookup. Try to use re.search or re.findall. E.g.:

>>> s = "29ca0a80180e9346295920344d64d1ce ::: 25basement"
>>> re.findall('(\S*) ', s)[0]
'29ca0a80180e9346295920344d64d1ce'
>>> re.search('(\S*) ', s).groups()
('29ca0a80180e9346295920344d64d1ce',)

BTW, this is not the task for regular expressions. Consider using some simple string operations (like split).

Comments

0

this regular expression seems to work

r"^(?:[^:]*\:)?([^:]*)(?::::.*)?$"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.