簡體   English   中英

Python Regex:在多行上匹配一個字符?

[英]Python Regex: Matching a character on multi line?

我正在pythonchallenge.com上做挑戰,我遇到了一般正則表達式的問題。

例如,如果我們有以下文字:

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

我想把字符a和b和c放到字符串中(來自上面的字符串)(但不是hello world)我該怎么做?

我知道我可以在python中執行以下操作:

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

但是,我遇到了正則表達式的問題。 我在正則表達式測試器上測試它,它似乎沒有做我想做的事情

這是我的正則表達式

<!--[a-z]*

根據我的理解,(在閱讀regex-expression.info教程之后)這個表達式應該找到指定字符串之后的所有字符:輸出abc

但是,這不起作用。 我的理解是,這也不是一個特殊字符,因為它不是[\\ ^ $。|?* +()中的任何一個。

如何使這個正則表達式表達式符合我的要求呢? 包括abc而不是hello world?

import re

su = '''hello world
xxxx hello world yyyy
<!--
_+]!yuyu*@&^}@?!hello world[@%]^@}$[*a*$&^!@(&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*$&^!@(&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*$& <!-- ^!@(&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']
>>> import re
>>> print strs = """hello world
<!--
%%$@_$^__#)^)&!_+]!*@&^}@[@%]()%+$&[(_@%+%$*^@$^!+]!&_#)_*}{}}!}_]$[%}@[{_@#_^{*
@##&{#&{&)*%(]{{([*}@[@&]+!!*{)!}{%+{))])[!^})+)$]#{*+^((@^@}$[*a*$&^{$!@#$%)!@(&bc"""
>>> re.findall(r'[a-zA-Z]+',strs.split('<!--')[-1])
['a', 'bc']

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM