开发者

regular expression: Efficiency of case insensitive comparsion

Given a pattern p and a string s, suppose p is in lower case. 开发者_运维技巧Which of the following two is more efficient?

r = re.compile(r'p', RE.IGNORECASE)
r.match(s)

... or ...

r = re.compile(r'p')
r.match(s.lower())


It's really going to depend on the language and engine. s.lower() and re.IGNORECASE are generally only slow because they're trying to deal with localization or Unicode strings (see this question). If the regex package you're using deals with that, and the s.lower() method doesn't, then the s.lower() method is a clear win. And vice-versa.

In general, I'd expect the s.lower() method to be faster (it tends to be more optimized than regex matching). But in the example as given ...

r = re.compile(r'[Pp]')
r.match(s)

... is going to be faster than either of them.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜