Solved

Stripping piece of text with RegEx (Part 2)

Posted on 2013-11-22
13
338 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
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…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

919 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

17 Experts available now in Live!

Get 1:1 Help Now