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 1054895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:28:42+00:00 2026-05-16T17:28:42+00:00

We are often told that regular expressions are slow and should be avoided whenever

  • 0

We are often told that regular expressions are slow and should be avoided whenever possible.

However, taking into account the overhead of doing some string manipulation oneself (not talking about algorithm mistakes – this is a different matter), especially in PHP or Perl (maybe Java) what is the limit, in which case can we consider string manipulation to be a better alternative? What regular expressions are particularly CPU greedy?

For instance, for the following, in C++, Java, PHP or Perl, what would you recommend?

The regular expressions would probably be faster:

  • s/abc/def/g or a ... while((i=index("abc",$x)>=0) ...$y .= substr()... based solution?
  • s/(\d)+/N/g or a scanning algorithm

But what about

  • an email validation regexp?
  • s/((0|\w)+?[xy]*[^xy]){2,7}/u/g

wouldn’t a handmade and specific algorithm be faster (while longer to write)?

edit

The point of the question is to determine what kind of regular expression would better be rewritten specifically for a given problem via string manipulation?

edit2

A common implementation is Perl regular expression. For instance in Perl – that requires to know how they are implemented – what kind of regular expression is to be avoided, because the implementation will make the process lengthy and ineffective? It may not be a complex regular expression.

edit July 2011 (based on comments)

I’m not saying all regular expressions are slow. Some particular regular expressions patterns are known to be slow, due to the particular processing their and due to their implementation.

In recent Perl / PHP implementations for instance, what is known to be rather slow – and should be avoided?

The answer is expected from people who did already their own research (profiler…) and who are able to provide a kind of general guidelines about what is recommended/to be avoided.

  • 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-16T17:28:43+00:00Added an answer on May 16, 2026 at 5:28 pm

    Who said regexes were slow? At least in Perl they tend to be the preferred method of manipulating strings.

    Regexes are bad at some things like email validation because the subject is too complex, not because they are slow. A proper email validation regex is over 6,000 characters long, and it doesn’t even handle all of the cases (you have to strip out comments first).

    At least in Perl 5, if it has a grammar it probably shouldn’t be parsed with one regex.

    You should also rewrite a regex as a custom function if the regex has grown to the point it can no longer be easily maintained (see the previous email validation example) or profiling shows that the regex is the slow component of your code.

    You seem to be concerned with the speed of the regex vs the custom algorithm, but that is not a valid concern until you prove that it is with a profiler. Write the code in the most maintainable way. If a regex is clear, then use a regex. If a custom algorithm is clear, then use a custom algorithm. If you find that either is eating up a lot of time after profiling your code, then start looking for alternatives.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We're often told we should protect encapsulation by making getter and setter methods (properties
Often, programmers write code that generates other code. (The technical term is metaprogramming ,
Often multiple applications share a large codebase of libraries that change often in development.
I don't develop in XCode very often, and recently MacPorts told me I needed
Often I add helper functions to my PHP that return complex HTML. For example,
Often I have some compile-time constant number that is also the upper limit of
Often times in unit tests I see people testing very simple things that realistically
Often while editing config files, I'll open one with vi and then when I
Often, I find myself wanting to write a unit test for a portion of
Often I find myself interacting with files in some way but after writing the

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.