Solved

Stripping piece of text with RegEx (Part 2)

Posted on 2013-11-22
13
331 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
Comment Utility
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 30

Expert Comment

by:Marco Gasi
Comment Utility
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
Comment Utility
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
 
LVL 10

Author Comment

by:cdebel
Comment Utility
@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
Comment Utility
@ddrudik: Same problem using your pattern (i've tried with the web site, and the sample i've just provided to marqusG).
0
 
LVL 30

Accepted Solution

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

\[[^\]]+?_BONJOUR\].+?\[\/.+?_BONJOUR\]
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 30

Expert Comment

by:Marco Gasi
Comment Utility
0
 
LVL 10

Author Comment

by:cdebel
Comment Utility
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
Comment Utility
@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
Comment Utility
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
Comment Utility
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
Comment Utility
I really got to look at this web site better next time :).  Thanks
0
 
LVL 27

Expert Comment

by:ddrudik
Comment Utility
Thank you.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
mapBully challenge 6 88
delphi parse string to params 3 81
Header of docx file 17 54
Add values of each row in an array 3 29
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

743 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now