python
  • regex
  • search
  • extract
  • digit
  • 2013-06-05 4 views 1 likes 
    1

    나는 문자열을하고 난발생 후 숫자 문자를 추출 하시겠습니까?

    내가 'exon_number'의 발생을 찾을 re.search를 사용하여 "X" "두 괄호에게"inbetween 인 exon_number을 추출 할하지만 난을 포함하지 않을 최종 출력에 문자열 exon_number

    예 :

    temp_ID = [] 
    
    k = '"gene_id ""XLOC_000001""; transcript_id ""TCONS_00000001""; exon_number ""1""; oId ""CUFF.17.1""; tss_id ""TSS1"";"'#input string 
    
    temp_ID.append((re.search(r'(exon_number\s""\d"")',k).group(1))) 
    
    print temp_ID 
    
    >['exon_number ""2""'] 
    
    
    desired_output = ['2'] 
    

    내가 그것을 한 자리/두 자리 숫자 일 수 있습니다 때문에 출력이 단지 두 " " inbetween 가치가되고 싶어요 그래서 난 못해 그

    temp_ID.append((re.search(r'(?<=exon_number\s"")\d{1,2}',k).group(0))) 
    

    문자를 먹지 않는 lookbehind, 당신이하지면 : [-3] 위치를 lect

    내가 어떤 다른

    당신은 lookbehind을 사용할 수 있습니다

    답변

    3

    당신은 당신의 괄호

    temp_ID.append((re.search(r'exon_number\s""(\d)""',k).group(1))) 
    

    를 이동해야하지만이 두 자릿수를 잡기 위해 원하는 경우

    temp_ID.append((re.search(r'exon_number\s""(\d+)""',k).group(1))) 
    

    편집으로 변경할 수 있습니다 :로 명확히 말하자면, 각 괄호 세트는 나중에 액세스 할 수있는 그룹이 될 것이며, \d+은 그것을 의미합니다. 1 자리 이상 일치

    +0

    감사합니다. 감사합니다. 이제 이해가된다. –

    0

    명확히해야하는 경우 알려주세요 경기에서 그들을 검색하십시오.

    1
    temp_ID.append((re.search(r'exon_number\s""(\d)""',k).group(1))) 
    

    http://docs.python.org/2/howto/regex.html#grouping

     관련 문제

    • 관련 문제 없음^_^