Which python re module to pick for translating a perl regular expression

perl, python

Solution

You could use

gene = this_re.group(1)

instead of

gene = info[this_re.start(0):this_re.end(0)]

By the way, the Python `re` module caches the `N` most recently used regex patterns, so (unless you have a very large number of patterns) it is not necessary to pre-compile it.

For Python 2.7, `re._MAXCACHE` (i.e. `N`) is 100.

Problem

I have the perl regular expression `/VA=\d+:(\S+):ENSG/` which is used in a if statement as ``` if ($info =~ /VA=\d+:(\S+):ENSG/){ $gene =$1; ``` I am trying to figure out what the best way to replicate this in python would be. Right now I have ``` gene_re = re.compile(r'VA=\d+:(\S+):ENSG') this_re = re.search(gene_re, info) if this_re is not None: gene = info[this_re.start(0):this_re.end(0)] ``` Is this a good way to translate it? I guess this is one area where perl is actually more readable than python. Note that the python regular expression is compiled because the next three lines are actually inside a loop.

Original source