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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T00:08:07+00:00 2026-05-22T00:08:07+00:00

I have this regex : ^((?:(?:\s*[a-zA-Z0-9]+)*)?)\s*function\s+([_a-zA-Z0-9]+)\s+\(\s*(.*)\s*\)\s* to match this string : public function private

  • 0

I have this regex :

^((?:(?:\s*[a-zA-Z0-9]+)*)?)\s*function\s+([_a-zA-Z0-9]+)\s+\(\s*(.*)\s*\)\s*

to match this string :

public function private ($var,Type $typed, $optional = 'option');

It works, but when it comes to match this one :

public function privateX ($var,Type $typed, $optional = 'option');

It fails.

I noticed that when the length of the function’s name exceeds 6 chars, it does not match anymore.

Here is the full code :

$strA = 'public function 6Chars ($var,Type $typed, $optional = "option");';
$strB = 'public function MoreThan7 ($var,Type $typed, $optional = "option");';

preg_match('!^((?:(?:\s*[a-zA-Z0-9]+)*)?)\s*function\s+([_a-zA-Z0-9]+)\s+\(\s*(.*)\s*\)\s*!',$strA,$mA);
preg_match('!^((?:(?:\s*[a-zA-Z0-9]+)*)?)\s*function\s+([_a-zA-Z0-9]+)\s+\(\s*(.*)\s*\)\s*!',$strB,$mB);

print_r($mA);
print_r($mB);

My Question is pretty simple : why the second string does not match ?

  • 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-22T00:08:07+00:00Added an answer on May 22, 2026 at 12:08 am

    I can’t reproduce this in RegexBuddy; both declarations do match. However, the steps needed by the regex engine to arrive at a match double with each character. A function name of 6 characters takes about 100.000 steps of the regex engine, 7 characters 200.000 steps, 8 characters 400.000 steps etc.

    Perhaps the regex engine gives up after a certain number of steps?

    A possessive quantifier (++) cuts down drastically on the number of steps needed by reducing the possible permutations the regex engine has to go through – 50 steps regardless of the length of the function name.

    !^((?:(?:\s*[a-zA-Z0-9]++)*)?)\s*function\s+([_a-zA-Z0-9]+)\s+\(\s*(.*)\s*\)\s*!
    

    The reason for the catastrophic backtracking you’re seeing in your regex is this:

    (?:(?:\s*[a-zA-Z0-9]+)*)
    

    You are nesting quantifiers, and you’ve made the spaces optional. Therefore ABC can be matched as ABC, A/BC, AB/C or A/B/C. The number of permutation rises exponentially with every character. You further complicate matters by making the entire group optional (the ? surrounding the whole thing).

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

Sidebar

Related Questions

I have this regex: private static final String SPACE_PATH_REGEX =[a-z|A-Z|0-9|\\/|\\-|\\_|\\+]+; I check if my
I have this regex that detects hashtags. It shouldn't match things with letters before
I have this regex working but now need to allow numbers without the decimal
I have this regex that is supposed to match 1='aa' or 1=aa or 1=aa,
I have this data: /blabla/blabla (abs,def) /yxz I use this regex (.*)(?:\(([^$]*)\))?\n But it
i have this regex string that i got from a website to pull emails
i am working on a regex match function in python. i have the following
I have a simple regex which checks an entire string for a function declaration.
I have this regex that checks for a 5 digit number ^\d{5}$ How do
I have this regex that uses forward and backward look-aheads: import re re.compile(<!inc\((?=.*?\)!>)|(?<=<!inc\(.*?)\)!>) I'm

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.