-1

I want to retrieve the value of DP from the following list 'seg'

 import re 
 seg = ['AC=2', 'AF=1.00', 'AN=2', 'DB', 'DP=6', 'FS=0.000',
 'MLEAC=2', 'MLEAF=1.00', 'MQ=31.55', 'MQ0=0', 'QD=31.64'] 

 for i in seg:
     name = re.compile(r'DP=(.*)')
     mo = name.search(i)
     if mo:
         print "True",mo
     else:
         print "no"

This gives me the output as follows:

no
no
no
no
True <_sre.SRE_Match object at 0x00000000025DDCD8>
no
no
no
no
no
no

The match is found but why is not giving me the DP value? Pls help me I am new to regex

Angie
  • 17
  • 5

2 Answers2

1

mo is an object. To get the DP value, use mo.group(1) to get the capture group. This will return 6, and you can parse that using int.

Michael Zhang
  • 1,445
  • 12
  • 14
  • mo.group(0) will return the string "DP=6", so if the goal is to use the value of DP, is it lacking something. We could add this function (found here [http://stackoverflow.com/questions/10365225/extract-digits-in-a-simple-way-from-a-python-string] ) to retrieve the float (or int, see the link) `DP=mo.group(0) def get_num(x): return float(''.join(ele for ele in x if ele.isdigit() or ele == '.')) DP = getnum(DP)` This will return `6.0` for the value of DP. It is less efficient than @nbryans answer though. – Morisa Jun 15 '16 at 14:00
  • 1
    Ah, I'm sorry. `mo.group(1)` will return 6. No need for finding integers :) I just edited the answer to reflect this. Nice catch! – Michael Zhang Jun 15 '16 at 14:04
1

Solution not using regex

for i in seg:
    if "DP=" in i:
        x, dp = i.split("=")
        print dp     # You can see we captured the 6

dp will contain the value you are looking for. You can append this into a list if you want to keep it.

If you want to keep using regex, you should use (as another commenter said) .group(0). You can see the discussion here for more information.

Community
  • 1
  • 1
nbryans
  • 1,507
  • 17
  • 24
  • hello bryans thanks for you answer. Actually I tried this it just gave an error, I am still in the learning phase. Could you please tell me the significance of x, in ur code? – Angie Jun 15 '16 at 14:42
  • Hi Angie, in the line `x, dp = i.split("=")`, we are splitting the string `i` (for example, "DP=6") using `=` as a delimiter. this produces a list as a result, ie `["DP","6"]` By having `x, dp`, we are having the first entry of that list ("DP") assigned to `x`, and the remainder of the list assigned to `dp`. Since the list is only 2 entries long, `dp` is assigned "6". What error in particular are you recieving? – nbryans Jun 15 '16 at 15:34