C-like赋值表达式?
您好,
我有一个if-elif链,其中我想要匹配一个字符串与
几个正则表达式。我也想在相应的elif ...块中使用匹配组
。我愿意使用的类似C的成语是:
if(match = my_re1.match(line):
#使用匹配
elsif(match = my_re2.match(line)):
#使用匹配
elsif(match = my_re3 .match(line))
#使用匹配
....在python中买这个是非法的。另一种方法是打开一个else:
阻止每个级别,完成任务然后测试。这个
不必要地导致越来越深的嵌套级别,我发现
丑陋就像在elif中首次测试RE一样难看:条款
然后,如果匹配,重新评估RE以访问比赛
组。
谢谢,
罗伯特
Hello,
I have an if-elif chain in which I''d like to match a string against
several regular expressions. Also I''d like to use the match groups
within the respective elif... block. The C-like idiom that I would
like to use is this:
if (match = my_re1.match(line):
# use match
elsif (match = my_re2.match(line)):
# use match
elsif (match = my_re3.match(line))
# use match
....buy this is illegal in python. The other way is to open up an else:
block in each level, do the assignment and then the test. This
unneccessarily leads to deeper and deeper nesting levels which I find
ugly. Just as ugly as first testing against the RE in the elif: clause
and then, if it matches, to re-evaluate the RE to access the match
groups.
Thanks,
robert
bo ******* @ googlemail.com 写道:
bo*******@googlemail.com wrote:
你好,
我有一个我想要的if-elif链匹配一个字符串对
几个正则表达式。我也想在相应的elif ...块中使用匹配组
。我愿意使用的类似C的成语是:
if(match = my_re1.match(line):
#使用匹配
elsif(match = my_re2.match(line)):
#使用匹配
elsif(match = my_re3 .match(line))
#使用匹配
...在python中买这个是非法的。另一种方法是打开一个else:块,执行任务然后测试。这个
不必要地导致越来越深的嵌套级别,我发现这很难看。就像在elif中首次测试RE一样丑陋:条款
然后,如果匹配,重新评估RE以访问匹配
组。
Hello,
I have an if-elif chain in which I''d like to match a string against
several regular expressions. Also I''d like to use the match groups
within the respective elif... block. The C-like idiom that I would
like to use is this:
if (match = my_re1.match(line):
# use match
elsif (match = my_re2.match(line)):
# use match
elsif (match = my_re3.match(line))
# use match
...buy this is illegal in python. The other way is to open up an else:
block in each level, do the assignment and then the test. This
unneccessarily leads to deeper and deeper nesting levels which I find
ugly. Just as ugly as first testing against the RE in the elif: clause
and then, if it matches, to re-evaluate the RE to access the match
groups.
这可能会有所帮助:
-----------
s =" foo"
类测试员(对象):
def __call __(自我,模式):
self.m = re.match(pattern,s)
返回self.m不是没有
def __ge tattr __(self,name):
return getattr(self.m,name)
test = Tester()
如果测试(bar):
打印错误
elif test(" foo"):
打印正确
-------------
Diez
This might help:
-----------
s = "foo"
class Tester(object):
def __call__(self, pattern):
self.m = re.match(pattern, s)
return self.m is not None
def __getattr__(self, name):
return getattr(self.m, name)
test = Tester()
if test("bar"):
print "wrong"
elif test("foo"):
print "right"
-------------
Diez
bo*******@googlemail.com 写道:
我有一个if-elif链,我希望将一个字符串与
匹配几个正则表达式。我也想在相应的elif ...块中使用匹配组
。我愿意使用的类似C的成语是:
if(match = my_re1.match(line):
#使用匹配
elsif(match = my_re2.match(line)):
#使用匹配
elsif(match = my_re3 .match(line))
#使用匹配
...在python中买这个是非法的。另一种方法是打开一个else:块,执行任务然后测试。这个
不必要地导致越来越深的嵌套级别,我发现这很难看。
I have an if-elif chain in which I''d like to match a string against
several regular expressions. Also I''d like to use the match groups
within the respective elif... block. The C-like idiom that I would
like to use is this:
if (match = my_re1.match(line):
# use match
elsif (match = my_re2.match(line)):
# use match
elsif (match = my_re3.match(line))
# use match
...buy this is illegal in python. The other way is to open up an else:
block in each level, do the assignment and then the test. This
unneccessarily leads to deeper and deeper nesting levels which I find
ugly.
这个(未经测试的)代码怎么样:
for re in(re1,re2,re3):
match = re.match(line)
如果匹配:
#使用它
这需要使用它对于每个正则表达式都意味着相同的
虽然...
Uli
- -
Sator Laser GmbH
Gesch?¤ftsf?? hrer:Thorsten F ?? cking,Amtsgericht Hamburg HR B62 932
How about this (untested) code:
for re in (re1, re2, re3):
match = re.match(line)
if match:
# use it
This requires that "use it" means the same for each regular expression
though...
Uli
--
Sator Laser GmbH
Gesch?¤ftsf??hrer: Thorsten F??cking, Amtsgericht Hamburg HR B62 932
bo ******* @ googlemail.com aécrit:
bo*******@googlemail.com a écrit :
您好,
我有一个if-elif链,我想在其中匹配一个字符串反对
几个正则表达式。我也想在相应的elif ...块中使用匹配组
。我愿意使用的类似C的成语是:
if(match = my_re1.match(line):
#使用匹配
elsif(match = my_re2.match(line)):
#使用匹配
elsif(match = my_re3 .match(line))
#use match
Hello,
I have an if-elif chain in which I''d like to match a string against
several regular expressions. Also I''d like to use the match groups
within the respective elif... block. The C-like idiom that I would
like to use is this:
if (match = my_re1.match(line):
# use match
elsif (match = my_re2.match(line)):
# use match
elsif (match = my_re3.match(line))
# use match
< ot>
不是吗第三次或第四次在这里出现同样的问题?
开始看起来像常见问题。
< / ot>
规范解决方案是遍历表达式列表,函数
对,即:
def use_match1(匹配):
#code here
def use_match2(匹配):
#code here
def use_match3 (匹配):
#code here
for exp,func in [
(my_re1,use_match1),>
(my_re2,use_match2),
(my_re3,use_match3)
]:
match = exp.match(line)
如果匹配:
func(匹配)
休息
替代解决方案是Diez的测试对象。
HTH
<ot>
Isn''t it the third or fourth time this very same question pops up here ?
Starts to look like a FAQ.
</ot>
The canonical solution is to iterate over a list of expression,function
pairs, ie:
def use_match1(match):
# code here
def use_match2(match):
# code here
def use_match3(match):
# code here
for exp, func in [
(my_re1, use_match1),
(my_re2, use_match2),
(my_re3, use_match3)
]:
match = exp.match(line)
if match:
func(match)
break
The alternate solution is Diez''s Test object.
HTH