Stripping piece of text with RegEx (Part 2)

My previous question was over simplified.  So here is what my real situation is.

I need to strip some part of a text using RegEx.

"[XXXX_BONJOUR]Everything here should be removed as well[/XXXX_BONJOUR]This part should remain"

As a result, i need "This part should remain".

Now what's particular about this version is that "XXXX" could be anything.  Don't worry about the begin & end matching.  Assume that they will match.

I've tried this expression, but it doesn't work:

Thank you
LVL 10
Christian de BellefeuilleProgrammerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

using System;
using System.Text.RegularExpressions;
namespace myapp
  class Class1
      static void Main(string[] args)
          String sourcestring = "[XXXX_BONJOUR]Everything here should be removed as well[/XXXX_BONJOUR]This part should remain";
          String matchpattern = @"\[([^_]+_BONJOUR)\].*?\[/\1\]";
          String replacementpattern = @"";

Open in new window

Marco GasiFreelancerCommented:
I've tested your regex on RegexBuddy and here ( it works like a charm. Can you show the code you're using?
I used my regex tester site to test the pattern and generate the code, here's a link to your example:

Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Christian de BellefeuilleProgrammerAuthor Commented:
@marqusG: As soon as i use a more complex source text, it doesn't work.

Here's my code in C++.  I also use the same web site as you have mentionned:
// ioText contain my text
boost::regex wExp("\\[.+?_BONJOUR\\].+?\\[\\/.+?_BONJOUR\\]");
cout << boost::regex_replace(ioText, wExp, "");

Open in new window

When i test it on myregextester, i get "NO MATCHES.  SOURCE TEXT UNCHANGED.  CHECK FOR DELIMITER COLLISION".  Of course i don't use "double backslash".

Here's a source text example

Here is some text with $PlaceHolders$.

To reach me: http://$PortalURL$

Some other text here#

Thank you
Christian de BellefeuilleProgrammerAuthor Commented:
@ddrudik: Same problem using your pattern (i've tried with the web site, and the sample i've just provided to marqusG).
Marco GasiFreelancerCommented:
See here if this makes sense for you: I've replaced your regex with the following:


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Marco GasiFreelancerCommented:
Christian de BellefeuilleProgrammerAuthor Commented:
Interresting.  I didn't knew that we could save the request.  How did you do that?

I see one difference in what you have done and what i've tested.  You seems to have checked the "S" flag (Dot matches all characters including newline)

Do you have any idea if i can set these flags with boost?
@cdebel, "." matches all characters including newline by default in Boost Regex (although that's not the case with respect to PHP or .NET--the two regex flavors supports).  To save examples, check the "Save example" box and enter the captcha details before clicking "Submit".  Check the "USE .NET" box to test with .NET regex (closest platform to yours; ignores pattern delimiter etc.).
Christian de BellefeuilleProgrammerAuthor Commented:
Thanks for these precisions ddrudik.  I was able to find the match_not_dot_newline flag, but couldn't find the opposite... so like you say, it's like this by default.  I didn't noticed the Save Example box too.

I've tried it with my application, and it seems to work perfectly.  

I wished i was able to understand regex like you guys, because that expression that marqus left, i have no clue what it mean.  Especially the bold part bellow


It would mean to me that there must be a "[", then "]" must be found right after it...
I've difficulties to understand the ^ and $ (Start and End of string) in regex.
Copy and paste that pattern into the pattern box on, check the "Explain" box and click "Submit".

From the "Explain" feature:
[^\]]+? any character except: '\]' (1 or more
times (matching the least amount
Christian de BellefeuilleProgrammerAuthor Commented:
I really got to look at this web site better next time :).  Thanks
Thank you.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Regular Expressions

From novice to tech pro — start learning today.