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

  • Home
  • SEARCH
  • 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 9190273
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:25:27+00:00 2026-06-17T20:25:27+00:00

What is the fastest performing regular expression that does not match any string? It

  • 0

What is the fastest performing regular expression that does not match any string? It may seem like a useless thing, but consider a program that takes a mandatory regex as a filter for instance (this is actually my scenario). I’ve tried a few and found b(?<!b) to be the best performer given that b occurs rarely in the input.

Here is a python code I wrote to test different patterns for their speed:

#!/usr/bin/env python

import re
import time

tests = [
  r'a\A',
  r'b\A',
  r'a^',
  r'b^',
  r'[^\s\S]',
  r'^(?<=a)',
  r'^(?<=b)',
  r'a(?<!a)',
  r'b(?<!b)',
  r'\Za',
  r'\Zb',
  r'$a',
  r'$b'
]
timing = []
text = 'a' * 50000000

for t in tests:
  pat = re.compile(t)
  start = time.time()
  pat.search(text)
  dur = time.time() - start
  timing.append((t, dur))

timing.sort(key=lambda x: x[1])
print('%-30s %s' % ('Pattern', 'Time'))
for t, dur in timing:
  print('%-30s %0.3f' % (t, dur))

On my machine, I get the following times:

Pattern                        Time
b(?<!b)                        0.043
b\A                            0.043
b^                             0.043
$a                             0.382
$b                             0.382
^(?<=a)                        0.395
\Za                            0.395
\Zb                            0.395
^(?<=b)                        0.414
a\A                            0.437
a^                             0.440
a(?<!a)                        0.796
[^\s\S]                        1.469

update: added benchmark for some of suggested regexes.

  • 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-06-17T20:25:28+00:00Added an answer on June 17, 2026 at 8:25 pm

    A single character is a valid regular expression. A single character that is not “magic” matches itself. If you can identify a single character that will never, ever appear in your text, you could make a pattern from that.

    How about ASCII NUL, character 0?

    I stuck in one more string in your test program, the string: '\0'

    It was about as fast as your best pattern: b(?<!b)

    Okay, you already have a character after the end of the string. How about a character before the start of the string? That’s impossible: 'x^'

    Aha! That’s faster than checking for a character after end of string. But it’s about as fast as your best pattern.

    I suggest replacing the b with an ASCII NUL and calling it good. When I tried that pattern: \0(?<!\0)

    It wins by a tiny fraction. But really, on my computer, all the ones discussed above are so close together that there isn’t much to distinguish them.

    Results:

    Pattern                        Time
    \0(?<!\0)                      0.098
    \0                             0.099
    x^                             0.099
    b(?<!b)                        0.099
    ^(?<=x)                        1.416
    $b                             1.446
    $a                             1.447
    \Za                            1.462
    \Zb                            1.465
    [^\s\S]                        2.280
    a(?<!a)                        2.843
    

    That was fun. Thanks for posting the question.

    EDIT: Ah hah! I rewrote the program to test with real input data, and got a different result.

    I downloaded “The Complete Works of William Shakespeare” from Project Gutenberg as a text file. (Weird, it gave an error on wget but let my browser get it… some sort of measure to protect against automated copying?) URL: http://www.gutenberg.org/cache/epub/100/pg100.txt

    Here are the results, followed by the modified program as I ran it.

    Pattern                        Time
    \0(?<!\0)                      0.110
    \0                             0.118
    x^                             0.119
    b(?<!b)                        0.143
    a(?<!a)                        0.275
    ^(?<=x)                        1.577
    $b                             1.605
    $a                             1.611
    \Za                            1.634
    \Zb                            1.634
    [^\s\S]                        2.441
    

    So yeah I’m definitely going with that first one.

    #!/usr/bin/env python
    
    import re
    import time
    
    tests = [
      r'x^',
      r'\0',
      r'[^\s\S]',
      r'^(?<=x)',
      r'a(?<!a)',
      r'b(?<!b)',
      r'\0(?<!\0)',
      r'\Za',
      r'\Zb',
      r'$a',
      r'$b'
    ]
    timing = []
    #text = 'a' * 50000000
    text = open("/tmp/pg100.txt").read()
    text = text * 10
    
    for t in tests:
      pat = re.compile(t)
      start = time.time()
      pat.search(text)
      dur = time.time() - start
      timing.append((t, dur))
    
    timing.sort(key=lambda x: x[1])
    print('%-30s %s' % ('Pattern', 'Time'))
    for t, dur in timing:
      print('%-30s %0.3f' % (t, dur))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

what's the fastest way to get only the important_stuff part from a string like
What's the fastest (best performing) way in PHP to transform a (My)SQL result like:
What's the fastest/best way to compare two arrays and return the difference? Much like
What is the fastest way to compare a string with an array of strings
What is the fastest way to check if a string contains some characters from
What's the fastest way to render dijit widgets? I know that the programmatic way
What is the fastest / clearest way to see if a string matches to
I'm using VB.NET. I am performing a select query that returns approximately 2500 rows,
What is the fastest language that runs on the JVM? Scala? Groovy? JRuby?
What is the absolute fastest way to hash a string in PHP? I have

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.