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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With