Solved

Stripping piece of text with RegEx (Part 2)

Posted on 2013-11-22
13
356 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
Question by:cdebel
  • 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 10

Author Comment

by:cdebel
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:cdebel
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 250 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:cdebel
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 250 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:cdebel
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:cdebel
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

828 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