Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6566317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:12:09+00:00 2026-05-25T14:12:09+00:00

s/(?P<head>\[\[foo[^\[]*)abc/\g<head>def s/(?=\[\[foo[^\[]*)abc/def Which is more efficient? Are there any other ways to make it

  • 0
s/(?P<head>\[\[foo[^\[]*)abc/\g<head>def

s/(?=\[\[foo[^\[]*)abc/def

Which is more efficient? Are there any other ways to make it more efficient? Please note that although I used Perl-style syntax for illustration purposes, I’m actually using Python’s re library, which doesn’t allow the \K (keep) keyword.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T14:12:10+00:00Added an answer on May 25, 2026 at 2:12 pm

    (?P<head>\[\[foo[^\[]*)abc in python using the re module is way faster:

    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
    

    Outputs:

    4.27380466461
    16.9591507912
    

    Edit (Run a few more times doing both calls in the same loop):

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

    Outputs:

    4.26199269295
    17.2384319305
    

    Edit (Fixing sub to match question):

    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
    

    Outputs:

    Run 1:
    4.62282061577
    17.8212277889
    
    Run 2:    
    4.6660721302
    17.1630160809
    
    Run 3:
    4.62124109268
    17.21393013
    

    Edit (with a string that will match the 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
    

    Output:

    23.4271130562
    29.6934807301
    

    And one final run:

    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)
    

    Output:

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Or any other tags :) for eg. <head> <title>page...</title> <script> var a = 'abc';
I have the following code at the head of a method: BigInteger foo =
Example 1: <head> <meta http-equiv=description content=<%= Foo %>/> </head> Renders <meta http-equiv=description content=Bar/> Example
head([Y],Y):-!. head([X|XS],X). I understand that the head of the list is stored on the
After reading the Head First Design Patterns book and using a number of other
I'm having problems wrapping my head around this. I have a function void foo(istream&
How can I overload class methods? I failed with: class D(object): def create(self): foo
Basically, what I want is git diff HEAD branch1 -- foo.txt > patch.txt; git
<head> <link href=foo.css rel=stylesheet type=text/css /> </head> can I via jquery change the href
I have a title doc.at('head/title').inner_html that comes out &amp; and it should be &

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.