2013-07-07 4 views
1

Я делаю проблемы на pythonchallenge.com, и у меня возникают проблемы с общим регулярным выражением.Python Regex: соответствие символа на нескольких строках?

Например, если мы имеем следующий текст:

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

И я хочу, чтобы получить символы а и Ь и с в строку (из приведенной выше строки) (но не привет мир), как может Я делаю это?

Я понимаю, что я могу сделать следующее питона:

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

Однако, у меня возникла проблемы с регулярным выражением. Я проверяю его на регулярном выражение тестера, и это, кажется, не делать то, что я хочу, чтобы это сделать

Вот моего выражение регулярного выражения

<!--[a-z]* 

Из моего понимания (после прочтения регулярок-выражения .info) это выражение должно найти все символы после указанной строки: вывод abc

Однако это не работает. Я понимаю, что это тоже не особый характер, поскольку он не является ни [\^$. |? * +().

Как я могу заставить это выражение регулярного выражения работать так, как я его хочу? Включить мир abc, но не hello?

+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'] 
Смежные вопросы