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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:27:55+00:00 2026-05-28T03:27:55+00:00

I think I may have overlooked something here, but I’m not sure why this

  • 0

I think I may have overlooked something here, but I’m not sure why this strange behaviour is occurring.

I know this is a nonsensical regex capture, but my question is why is this occurring.

<?php
// < 15 "a" characters returns "aaaaaaaaaaaaaa {" (no matches)
$code = 'aaaaaaaaaaaaaa {';
// 15 "a" characters returns NULL
$code = 'aaaaaaaaaaaaaaa {';

$code = preg_replace('#(a+)+\{#', 'b', $code);
var_dump($code);
?>

I realise that the regex has a \s missing. If I change the regex to #(a+)+\s\{# I get "b". But why don’t I get "aaaaaaaaaaaaaaa {" with 15 “a” characters instead of a NULL? The manual states that NULL is returned when an error occurs, but nothing is being printed out by PHP (error checking is on).

EDIT:

The reason I am using (a+)+ is because I am trying to simplify the problem down to the basic level. I started off with this:

I want to capture a series of non-whitespace characters followed by spaces followed by an opening brace.

this is some text {

So I used the regex:

#([^\s\{\}]+\s*)+\{#msi

Which also uses the double +. Is there a better way, and why is this causing an issue in the first place?

EDIT 2:
From the answers below, it appears that the double + causes a lot of backreferences and should be avoided. I have opted for this [^\s\{\}][^\{\}]*+\{.

  • 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-28T03:27:55+00:00Added an answer on May 28, 2026 at 3:27 am
    <?php
    
    ini_set('pcre.backtrack_limit',20000);
    $code = 'aaaaaaaaaaaaaaa {';
    $code = preg_replace('#(a+)+\{#', 'b', $code);
    var_dump($code, preg_last_error());
    
    ini_set('pcre.backtrack_limit',1000000);
    $code = 'aaaaaaaaaaaaaaa {';
    $code = preg_replace('#(a+)+\{#', 'b', $code);
    var_dump($code, preg_last_error());
    

    Result:

    NULL
    int(2)
    string(17) "aaaaaaaaaaaaaaa {"
    int(0)
    

    But you really should use either (a+) or (a)+, not a combination of both.

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

Sidebar

Related Questions

http://pastebin.com/mYk8M038 here is what I have so far... I think it may be something
I think I may have asked this on Haskell-Cafe at some point, but damned
I think it may have something to do with the external file but I
I think I may have stumbled onto a limitation of Flot, but I'm not
I think I may have implemented this incorrectly because the results do not make
I think I may have used a repeater when I should have used something
I think this may be relatively straight forward. I have a rewrite rule that
Hopefully this question isn't as subjective as I think it may be. I have
I think it may have to do with TextMate, but we work in a
I think I may have found a bug in WebMatrix's PageData, but I am

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.