假设我有一个工作流,其中涉及检查一个长字符串的开头(LS
比如说),看看它是否以一个较短的字符串开头SS
。如果是这样,我砍掉匹配的部分LS
并对剩余的部分做一些事情。否则,我会做其他事情。(引发这个问题的具体案例是一个解析库。)
def do_thing(LS, SS):
if (LS.startswith(SS)):
action_on_match(LS[len(SS):])
else:
action_on_no_match()
这很简单。不过,现在假设我想做同样的事情,但这次我希望字符串不区分大小写。可以测试是否“LS.startswith(SS)
但不区分大小写”。但是当我将它传递给 时,我应该如何确定LS
要“砍掉”action_on_match()
多少?像以前一样使用是不够的len(SS)
,因为如果我是大写或小写或大小写折叠的东西,那么匹配前缀的长度LS
可能不是我所期望的:改变字符串的大小写可以改变它的长度. 重要的是,LS
传递的部分action_on_match()
与程序作为输入接收的内容完全相同(当然,在截止点之后)。
回答者建议使用lower()
并保留使用len(SS)
,但这不起作用:
Python 3.4.2 (v3.4.2:ab2c023a9432, Oct 6 2014, 22:15:05) [MSC v.1600 32 bit (In
tel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> def action_on_match (s): return "Match: %s" % s
...
>>> def action_on_no_match (): return "No match"
...
>>> def do_thing (LS, SS):
... if LS.lower().startswith(SS.lower()):
... return action_on_match(LS[len(SS):])
... else:
... return action_on_no_match()
...
>>> do_thing('i\u0307asdf', '\u0130')
'Match: \u0307asdf'
>>>
在这里我们希望看到'Match: asdf'
,但是有一个额外的字符。