2013-07-07 5 views
1

나는 pythonchallenge.com에 도전하고 있으며 일반 정규식에 문제가 있습니다. 예를 들어Python 정규식 : 여러 줄에있는 문자를 일치 시키시겠습니까?

우리는 다음과 같은 텍스트가있는 경우 : 어떻게

hello world 
<!-- 
%%[email protected]_$^__#)^)&!_+]!*@&^}@[@%]()%+$&[([email protected]%+%$*^@$^!+]!&_#)_*}{}}!}_]$[%}@[{[email protected]#_^{* 
@##&{#&{&)*%(]{{([*}@[@&]+!!*{)!}{%+{))])[!^})+)$]#{*+^((@^@}$[*a*$&^{[email protected]#$%)[email protected](&bc 

을 그리고 난 A와 B와 C (위의 문자열에서) 캐릭터 라인 (그러나 안녕하세요!) 문자를 취득 할 수있는 나는 이것을한다?

은 내가 파이썬에서 다음을 수행 할 수 있습니다 이해 :

x = "".join(re.findall("regex", data)) 

을하지만, 내가 정규식 표현에 문제가 있어요. 내가 정규식 테스터에 그것을 밖으로 테스트입니다, 그리고 내가 여기

를하고 싶은 일을하는 것이하지 않는 것 나의 이해에서 내 정규식 표현

<!--[a-z]* 

이다, (정규식 표현을 읽은 후 .info 자습서)이 표현식은 지정된 문자열 뒤의 모든 문자를 찾아야합니다. abc

그러나 이것은 작동하지 않습니다. [\^$. |? * +() 중 하나가 아니기 때문에 이것은 특별한 성격이 아닙니다.

이 정규 표현식을 원하는 방식으로 작동 시키려면 어떻게해야합니까? abc하지만 hello world는 포함시키지 않으십니까?

+0

_ " eyquem

답변

2
import re 

su = '''hello world 
xxxx hello world yyyy 
<!-- 
_+]!yuyu*@&^}@?!hello world[@%]^@}$[*a*$&^[email protected](&bc??,=hello''' 

print su 

pat = '([a-z]+)(?![a-z])(?<!world)' 
print "\nexcluding all the words 'world'\n%s" % pat 
print re.findall(pat,su) 

pat = '([a-z]+)(?![a-z])(?<!\Ahello world)' 
print "\nexcluding the word 'world' of the starting string 'hello world'\n%s" % pat 
print re.findall(pat,su) 

pat = '([a-z]+)(?![a-z])(?<!hello world)' 
print "\nexcluding all the words 'world' of a string 'hello world'\n%s" % pat 
print re.findall(pat,su) 

print '\n-----------' 

pat = '([a-z]+)(?![a-z])(?<!hello)' 
print "\nexcluding all the words 'hello'\n%s" % pat 
print re.findall(pat,su) 

pat = '([a-z]+)(?![a-z])(?<!\Ahello)' 
print "\nexcluding the starting word 'hello'\n%s" % pat 
print re.findall(pat,su) 

pat = '([a-z]+)(?![a-z])(?<!hello(?= world))' 
print "\nexcluding all the words 'hello' of a string 'hello world'\n%s" % pat 
print re.findall(pat,su) 

print '\n-----------' 

pat = '([a-z]+)(?![a-z])(?<!hello|world)' 
print "\nexcluding all the words 'hello' and 'world'\n%s" % pat 
print re.findall(pat,su) 

pat = '([a-z]+)(?![a-z])(?<!hello(?= world))(?<!hello world)' 
print "\nexcluding all the words of a string 'hello world'\n%s" % pat 
print re.findall(pat,su) 

pat = '([a-z]+)(?![a-z])(?<!\Ahello(?= world))(?<!\Ahello world)' 
print "\nexcluding all the words of the starting string 'hello world'\n%s" % pat 
print re.findall(pat,su) 

결과

hello world 
xxxx hello world yyyy 
<!-- 
_+]!yuyu*@&^}@?!hello world[@%]^@}$[*a*$&^[email protected](&bc??,=hello 

excluding all the words 'world' 
([a-z]+)(?![a-z])(?<!world) 
['hello', 'xxxx', 'hello', 'yyyy', 'yuyu', 'hello', 'a', 'bc', 'hello'] 

excluding the word 'world' of the starting string 'hello world' 
([a-z]+)(?![a-z])(?<!\Ahello world) 
['hello', 'xxxx', 'hello', 'world', 'yyyy', 'yuyu', 'hello', 'world', 'a', 'bc', 'hello'] 

excluding all the words 'world' of a string 'hello world' 
([a-z]+)(?![a-z])(?<!hello world) 
['hello', 'xxxx', 'hello', 'yyyy', 'yuyu', 'hello', 'a', 'bc', 'hello'] 

----------- 

excluding all the words 'hello' 
([a-z]+)(?![a-z])(?<!hello) 
['world', 'xxxx', 'world', 'yyyy', 'yuyu', 'world', 'a', 'bc'] 

excluding the starting word 'hello' 
([a-z]+)(?![a-z])(?<!\Ahello) 
['world', 'xxxx', 'hello', 'world', 'yyyy', 'yuyu', 'hello', 'world', 'a', 'bc', 'hello'] 

excluding all the words 'hello' of a string 'hello world' 
([a-z]+)(?![a-z])(?<!hello(?= world)) 
['world', 'xxxx', 'world', 'yyyy', 'yuyu', 'world', 'a', 'bc', 'hello'] 

----------- 

excluding all the words 'hello' and 'world' 
([a-z]+)(?![a-z])(?<!hello|world) 
['xxxx', 'yyyy', 'yuyu', 'a', 'bc'] 

excluding all the words of a string 'hello world' 
([a-z]+)(?![a-z])(?<!hello(?= world))(?<!hello world) 
['xxxx', 'yyyy', 'yuyu', 'a', 'bc', 'hello'] 

excluding all the words of the starting string 'hello world' 
([a-z]+)(?![a-z])(?<!\Ahello(?= world))(?<!\Ahello world) 
['xxxx', 'hello', 'world', 'yyyy', 'yuyu', 'hello', 'world', 'a', 'bc', 'hello'] 

그리고 당신은 분석 된 문자열에서 특정 패턴 후 잡을하려면 :

print su 

print "\ncatching all the lettered strings after <!--" 
print "re.compile('^.+?<!--|([a-z]+)',re.DOTALL)" 
rgx = re.compile('^.+?<!--|([a-z]+)',re.DOTALL) 
print [x.group(1) for x in rgx.finditer(su) if x.group(1)] 

print ("\ncatching all the lettered strings after <!--\n" 
     "excluding all the words 'world'") 
print "re.compile('^.+?<!--|([a-z]+)(?<!world)',re.DOTALL)" 
rgx = re.compile('^.+?<!--|([a-z]+)(?![a-z])(?<!world)',re.DOTALL) 
print [x.group(1) for x in rgx.finditer(su) if x.group(1)] 

print ("\ncatching all the lettered strings after <!--\n" 
     "excluding all the words 'hello'") 
print "re.compile('^.+?<!--|([a-z]+)(?<!hello)',re.DOTALL)" 
rgx = re.compile('^.+?<!--|([a-z]+)(?![a-z])(?<!hello)',re.DOTALL) 
print [x.group(1) for x in rgx.finditer(su) if x.group(1)] 

print ("\ncatching all the lettered strings after <!--\n" 
     "excluding all the words 'hello' belonging to a string 'hello world'") 
print "re.compile('^.+?<!--|([a-z]+)(?<!hello(?= world))',re.DOTALL)" 
rgx = re.compile('^.+?<!--|([a-z]+)(?![a-z])(?<!hello(?= world))',re.DOTALL) 
print [x.group(1) for x in rgx.finditer(su) if x.group(1)] 

결과

hello world 
xxxx hello world yyyy 
<!-- 
_+]!yuyu*@&^}@?!hello world[@%]^@}$[*a*$& <!-- ^[email protected](&bc??,=hello 

catching all the lettered strings after first <!-- 
re.compile('.+?<!--|([a-z]+)',re.DOTALL) 
['yuyu', 'hello', 'world', 'a', 'bc', 'hello'] 

catching all the lettered strings after first <!-- 
excluding all the words 'world' 
re.compile('.+?<!--|([a-z]+)(?<!world)',re.DOTALL) 
['yuyu', 'hello', 'a', 'bc', 'hello'] 

catching all the lettered strings after first <!-- 
excluding all the words 'hello' 
re.compile('.+?<!--|([a-z]+)(?<!hello)',re.DOTALL) 
['yuyu', 'world', 'a', 'bc'] 

catching all the lettered strings after first <!-- 
excluding all the words 'hello' belonging to a string 'hello world' 
re.compile('.+?<!--|([a-z]+)(?<!hello(?= world))',re.DOTALL) 
['yuyu', 'world', 'a', 'bc', 'hello'] 
관련 문제