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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T03:31:12+00:00 2026-05-31T03:31:12+00:00

If I have a string of the form: $text=’remove1 (keep1) remove2 (keep2) remove3′; Using

  • 0

If I have a string of the form:

$text='remove1  (keep1) remove2 (keep2) remove3';

Using the response

https://stackoverflow.com/a/5293343/1154853

$re = '/[^()]*+(\((?:[^()]++|(?1))*\))[^()]*+/';
$text = preg_replace($re, '$1', $text);

I get

(keep1)(keep2)

If I want to go from

$text='remove1  {keep1} remove2 {keep2} remove3';

to

{keep1}{keep2}

that is I want to change the delimiters, how do I change the given regular expression? I tried all sorts of combinations of changing ( to { but I couldn’t get it to work. I find these reg exp pretty tough!

  • 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-31T03:31:14+00:00Added an answer on May 31, 2026 at 3:31 am

    For parentheses (), you could use:

    $re = '/(?:^|(?<=\)))[^(]+/';
    $text = preg_replace($re, '', $text);
    

    EDIT For braces {}, use this variant:

    $re = '/(?:^|(?<=\}))[^{]+/';
    $text = preg_replace($re, '', $text);
    

    Note that these will fail – as all regex does – for nested structures.

    Explanation

    (?:        # non-capturing group start
      ^        #   start-of-string
      |        #   or...
      (?<=\})  #   look-behind: a position preceded by a } closing brace
    )          # end non-capturing group
    [^{]+      # any character except an opening brace, multiple times
    

    The non-capturing group fixes the start of the match behind a closing brace or at the start of the string. The [^{]+ matches every character up to the next brace. Effectively this matches everything in-between {words}.

    See it: http://rubular.com/r/1Sp72TbHIi

    EDIT #2 Of course, thinking less complicated, this would also work:

    $re = '/((?:\\\w+)?\{[^}]*\})/';
    $text = preg_replace($re, '$1', $text);
    

    http://rubular.com/r/rPLMbCLbcq

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

Sidebar

Related Questions

I have a form contains text fields,a textView and 2 switches.Now I am using
I have string coming in through an SSIS package via text file in form:
I have several strings in the rough form: [some text] [some number] [some more
I have a string of the form foo-bar-1.23-4, and I need to split at
I have an array of integers in string form: var arr = new string[]
I want to store data in string form to MySQL. I have created the
I have a string which contain tags in the form < tag > .
I have a date string of the form '2009/05/13 19:19:30 -0400'. It seems that
I have a situation where I need to concatenate several string to form an
I have a form that sometimes gets linked to with some query string parameters.

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.