2013-05-28 4 views
0

나는 일생 일대의 것으로 보이고 작동시킬 수없는 코드를 만들었습니다.파이썬 : re.sub 패턴과 대체 와일드 카드

pattern = "\[([a-zA-Z0-9].*?)#([a-zA-Z0-9].*?)\]" 
pattern_obj = re.compile(pattern, re.MULTILINE) 
translation = pattern_obj.sub("<ol>\\1</ol>", translation) 

는 내가 여기서 뭘하려고하는 텍스트를 변경할 수있다, 즉 :

[ 
    # This is item number one in an ordered list. # 

    # And this is item number two in the same list. # 
] 

속으로 : 기본적으로

<ol> 
#This is item number one in an ordered list. # 
#And this is item number two in the same list. # 
</ol> 

,이 사이의 모든 텍스트를 식별하도록되어 [및] 텍스트의 어딘가에 #를 붙이고 [내부 문자]를 및 </ol>으로 변경하고 모든 내부 텍스트를 동일하게 유지하십시오. 아무도 조언을 해줄 수 있습니까?

미리 감사드립니다.

답변

0

이 수행 거의 당신이 원하는 :

>>> re.compile(r"\[([^\]]*)\]").sub("<ol>\\1</ol>", "b[#a]c") 
'b<ol>#a</ol>c' 

[^\]]\[ clsing 브래킷 후 제외한 모든 문자]를 걸릴. 그것은 다음과 같이 것 인 #

: 당신이 뭔가 사이에 뭔가를 찾으려면

>>> re.compile(r"\[([^\]]*#[^\]]*)\]").sub("<ol>\\1</ol>", "b[#a]c") 
'b<ol>#a</ol>c' 
>>> re.compile(r"\[([^\]]*#[^\]]*)\]").sub("<ol>\\1</ol>", "b[gggg]c") 
'b[gggg]c' 

.는 항상 약간의 문제이다.

+0

감사합니다. 문제가 해결되었습니다. – user2421580