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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T11:38:07+00:00 2026-05-18T11:38:07+00:00

I have the following sample text The quick brown {fox} jumps over the lazy

  • 0

I have the following sample text

The quick brown {fox} jumps over the lazy {dog}

I need to match any string enclosed in {} which can occur several times in the text.
I tried the following code but it doesn’t work properly

<?php

$matches = array();

$string = "The quick brown {fox} jumps over the lazy {dog}";

preg_match("/\{(.*)\}/",$string,$matches);

print_r($matches);

?>

and that’s what I get

Array
(
    [0] => {fox} jumps over the lazy {dog}
    [1] => fox} jumps over the lazy {dog
)

I would expect getting

Array
(
    [0] => {fox} jumps over the lazy {dog}
    [1] => fox
    [2] => dog
)

So how can I force PHP to match the nearest “}” instead of matching the last one ?

  • 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-18T11:38:07+00:00Added an answer on May 18, 2026 at 11:38 am

    Your existing regex has .* which is greedy and tries to consume as much as possible. To fix this you need to make the regex non-greedy by adding a ? at the end as:

    .*?
    

    alternatively you can also use [^}]* in place of .*.

    And since you want all matches, you need to use preg_match_all

    See it

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

Sidebar

Related Questions

I have the following input data in String: Hello #this# is #sample# text. It
I have the following String Sample however in some occurences there may be one
I have the following formatted sample string: == header == information about things ==headeragain==
1I have the following two tables (sample data) and need to be able to
I have the following sample text and I want to replace '[core].' with something
If I have the following (sample) text file; year,2008,2009,2010 income,1000,1500,2000 dividends,100,200,300 net profit,1100,1700,2300 expenses,500,600,500
I have the following sample nested XML <?xml version=1.0 encoding=UTF-8?> <text> <main> <div n=Section
I have the following json output when i call a sample webservice <string>[{Name:Ajay Singh,Company:Birlasoft
I have the following code var aVar = sample text; alert(aVar); $(this).replaceWith( <input type='text'
I have the following XML sample: <finding> <title>Found something</title> <heading>Severity:</heading> <text>Really low.</text> <heading>URL:</heading> <text>https://www.something.com:443</text>

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.