哪个正则表达式更有效?

Kud*_*udu 3 python regex

s/(?P<head>\[\[foo[^\[]*)abc/\g<head>def
Run Code Online (Sandbox Code Playgroud)

s/(?=\[\[foo[^\[]*)abc/def
Run Code Online (Sandbox Code Playgroud)

哪个更有效率?还有其他方法可以提高效率吗?请注意,虽然我使用Perl风格的语法进行说明,但实际上我使用的是Python的re库,它不允许使用\K(keep)关键字.

cho*_*own 6

(?P<head>\[\[foo[^\[]*)abc在python中使用re模块更快:

import time
import re

rec1 = re.compile('(?P<head>\[\[foo[^\[]*)abc')
rec2 = re.compile('(?=\[\[foo[^\[]*)abc')

total1, total2 = 0.0, 0.0

def timeRE(ver):
    x = ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_1234567890_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" * 100)
    t1 = time.time()
    if ver is 1:
        rec1.sub("def", x)
    else:
        rec2.sub("def", x)
    return (time.time() - t1)

for x in xrange(50000):
    total1 += timeRE(1)

for x in xrange(50000):
    total2 += timeRE(2)

print total1
print total2
Run Code Online (Sandbox Code Playgroud)

输出:

4.27380466461
16.9591507912
Run Code Online (Sandbox Code Playgroud)

编辑(在同一循环中再次运行两次):

for x in xrange(50000):
    total1 += timeRE(1)
    total2 += timeRE(2)
Run Code Online (Sandbox Code Playgroud)

输出:

4.26199269295
17.2384319305
Run Code Online (Sandbox Code Playgroud)

编辑(修复子以匹配问题):

import time
import re
rec1 = re.compile('(?P<head>\[\[foo[^\[]*)abc')
rec2 = re.compile('(?=\[\[foo[^\[]*)abc')
total1, total2 = 0.0, 0.0
def timeRE(ver):
    x = ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_1234567890_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" * 100)
    t1 = time.time()
    if ver is 1:
        rec1.sub("\g<head>def", x)
    else:
        rec2.sub("def", x)
    return (time.time() - t1)

for x in xrange(50000):
    total1 += timeRE(1)
    total2 += timeRE(2)
print total1
print total2
Run Code Online (Sandbox Code Playgroud)

输出:

Run 1:
4.62282061577
17.8212277889

Run 2:    
4.6660721302
17.1630160809

Run 3:
4.62124109268
17.21393013
Run Code Online (Sandbox Code Playgroud)

编辑(使用与REGEX匹配的字符串):

import time
import re

rec1 = re.compile('(?P<head>\[\[foo[^\[]*)abc')
rec2 = re.compile('(?=\[\[foo[^\[]*)abc')
total1, total2 = 0.0, 0.0

def timeRE(ver):
    x = ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_1234567890_<head>_<tail>_</head>_</tail>_abcdefghijklmnopqrstuvwxyz_<head>[[fooBAR_ABCDEFGHIJKLMNOPQRSTUVWXYZ_abc]]]]defghiojklmnopqrstuvwyz" * 100)
    t1 = time.time()
    if ver is 1:
        rec1.sub("\g<head>def", x)
    else:
        rec2.sub("def", x)
    return (time.time() - t1)

for x in xrange(50000):
    total1 += timeRE(1)
    total2 += timeRE(2)

print total1
print total2
Run Code Online (Sandbox Code Playgroud)

输出:

23.4271130562
29.6934807301
Run Code Online (Sandbox Code Playgroud)

最后一次:

import time
import re
rec1 = re.compile('(?P<head>\[\[foo[^\[]*)abc')
rec2 = re.compile('(?=\[\[foo[^\[]*)abc')
total1, total2 = 0.0, 0.0
def timeRE(ver):
    x = ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_1234567890_<head>_<tail>_</head>_</tail>_abcdefghijklmnopqrstuvwxyz_<head>[[fooBAR_ABCDEFGHIJKLMNOPQRSTUVWXYZ_abc]]]]defghiojklmnopqrstuvwyz" * 100)
    t1 = time.time()
    if ver is 1:
        rec1.sub("\g<head>def", x)
    else:
        rec2.sub("def", x)
    return (time.time() - t1)
for x in xrange(50000):
    total1 += timeRE(1)
    total2 += timeRE(2)
print "Method 1: Avg run took: %+0.7f - With a total of: %+0.7f" % ((total1 / 50000.0), total1)
print "Method 2: Avg run took: %+0.7f - With a total of: %+0.7f" % ((total2 / 50000.0), total2)
Run Code Online (Sandbox Code Playgroud)

输出:

Method 1: Avg run took: +0.0004924 - With a total of: +24.6196477
Method 2: Avg run took: +0.0005921 - With a total of: +29.6053855
Run Code Online (Sandbox Code Playgroud)

  • 好吧,你已经得到我的+1,但只是fyi,第二个正则表达式(由于OP的错误,而不是你)将永远不会匹配任何东西,因此不会进行任何替换.这是因为它正在查看`[[foo`然后尝试匹配`abc`.除了不允许使用可变长度的lookbehinds之外,lookbehind可以修复它. (2认同)