Skip to content Skip to sidebar Skip to footer

How To Use Regular Expressions In Python?

Hopefully someone can help, I'm trying to use a regular expression to extract something from a string that occurs after a pattern, but it's not working and I'm not sure why. The re

Solution 1:

Use .group() on the search result to print the captured groups:

>>>print(x.group(0))
YP_001405731.1

As Martijn has had pointed out, you created a match object. The regular expression is correct. If it was wrong, print(x) would have printed None.

Solution 2:

You should probably think about re-writing your regex so that you find all pairs so you don't have to muck around with specific groups and hard-coded look behinds...

import re
kv = dict(re.findall('(\w+)=([^;]+)', s))
# {'gbkey': 'CDS', 'product': 'carboxynorspermidinedecarboxylase', 'protein_id': 'YP_001405731.1'}
print kv['protein_id']
# YP_001405731.1

Post a Comment for "How To Use Regular Expressions In Python?"