gpt4 book ai didi

Python 正则表达式 : Matching a character on multi line?

转载 作者:行者123 更新时间:2023-11-30 23:35:12 25 4
gpt4 key购买 nike

我正在 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']

关于Python 正则表达式 : Matching a character on multi line?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17511278/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com