Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Stripping piece of text with RegEx (Part 2)

Posted on 2013-11-22
13
Medium Priority
?
403 Views
Last Modified: 2013-11-22
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.

Example:
"[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:
\[.+?_BONJOUR\].+?\[\/.+?_BONJOUR\]

Thank you
0
Comment
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
  • 3
13 Comments
 
LVL 27

Expert Comment

by:ddrudik
ID: 39669310
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 = @"";
          Console.WriteLine(Regex.Replace(sourcestring,matchpattern,replacementpattern));
        }
    }
}

Open in new window

0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39669367
I've tested your regex on RegexBuddy and here (http://www.myregextester.com/index.php): it works like a charm. Can you show the code you're using?
0
 
LVL 27

Expert Comment

by:ddrudik
ID: 39669386
I used my regex tester site to test the pattern and generate the code, here's a link to your example:

http://www.myregextester.com/?r=a54afc16

Thanks.
-Doug.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 10

Author Comment

by:Christian de Bellefeuille
ID: 39669729
@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
Hi,

Here is some text with $PlaceHolders$.

[DETAIL]
To reach me: http://$PortalURL$
[/DETAIL]

[SOMETHING_BONJOUR]
Some other text here#
[/SOMETHING_BONJOUR]

Thank you
0
 
LVL 10

Author Comment

by:Christian de Bellefeuille
ID: 39669732
@ddrudik: Same problem using your pattern (i've tried with the web site, and the sample i've just provided to marqusG).
0
 
LVL 31

Accepted Solution

by:
Marco Gasi earned 1000 total points
ID: 39669774
See here if this makes sense for you: I've replaced your regex with the following:

\[[^\]]+?_BONJOUR\].+?\[\/.+?_BONJOUR\]
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39669775
0
 
LVL 10

Author Comment

by:Christian de Bellefeuille
ID: 39669873
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?
0
 
LVL 27

Assisted Solution

by:ddrudik
ddrudik earned 1000 total points
ID: 39669922
@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 myregextester.com 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.).
0
 
LVL 10

Author Comment

by:Christian de Bellefeuille
ID: 39669966
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

\[[^\]]+?_BONJOUR\].+?\[\/.+?_BONJOUR\]

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.
0
 
LVL 27

Expert Comment

by:ddrudik
ID: 39669979
Copy and paste that pattern into the pattern box on myregextester.com, check the "Explain" box and click "Submit".

From the "Explain" feature:
[^\]]+? any character except: '\]' (1 or more
times (matching the least amount
possible))
0
 
LVL 10

Author Comment

by:Christian de Bellefeuille
ID: 39670000
I really got to look at this web site better next time :).  Thanks
0
 
LVL 27

Expert Comment

by:ddrudik
ID: 39670021
Thank you.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
We are witnesses that everyone is saying that our children shouldn't "play" with a technology because it is dangerous. This article is going to prove that they are wrong.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question