Solved

Strip parenthesis and contents

Posted on 2009-05-13
6
284 Views
Last Modified: 2013-12-17
Dear Experts,

In one of our transaction, we add something like "(Free 12BX) or (Free ##IU)" as a note in addition to the product description as:

Borgesic Forte, tablet 100's (Free ##IU)  -- ## is the quantity and IU is the Issue Unit (could be BX, PC, EA, FT, Etc)

In the issuance transaction however, I want to strip out "(Free ##IU)" to keep the original description as:

Borgesic Forte, tablet 100's

So basically what I need is stripping:

"Borgesic Forte, tablet 100's (Free ##IU)"  to "Borgesic Forte, tablet 100's"  

where "(Free"  is constant and "##IU" varies.  


Thanks.
0
Comment
Question by:JaimeJegonia
[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
  • 4
6 Comments
 
LVL 5

Assisted Solution

by:Aanvik
Aanvik earned 25 total points
ID: 24380740
Try replace function. Replace with blank string. You can also use Substring function.

http://msdn.microsoft.com/en-us/library/fk49wtc1.aspx
0
 
LVL 39

Accepted Solution

by:
abel earned 275 total points
ID: 24380753
Assuming that the string ends with a closing parenthesis and assuming that there are not other closing parentheses between the opening and the closing parentheses, you can do this:

// replace anything between the parentheses, incl. leading space and any content
string newString = Regex.Replace("Borgesic Forte, tablet 100's (Free ##IU)", @"\s*\([^)]+\)$", "");

Open in new window

0
 
LVL 39

Expert Comment

by:abel
ID: 24380754
Sorry, Aanvik, missed your post.
0
Turn Insights Into Action

You’ve already invested in ITSM tools, chat applications, automation utilities, and more. Fortify these solutions with intelligent communications so you can drive business processes forward.

With xMatters, you'll never miss a beat.

 
LVL 39

Expert Comment

by:abel
ID: 24380774
Anyway, if you are not accustomed to regexes, it can look daunting at first (you may need a using System.Text on top of your class), so let's explain them a bit:

@"..."
the opening @-sign makes the string literal, no need for escaping backslashes as "\\" anymore
\s*
the "\s" means whitespace and the * means zero or more

\(
the "(" is special in a regex. To use the literal, it must be escaped, hence "\("
[^)]+
the tricky bit: grab everything not a closing a parenthesis and grab one or more (the + sign)
\)
the closing parenthesis needs to be escaped too

$
the dollar sign means "match end of string". That means that closing parenthesis must be at the end of the string for the replacement to work. If that is not always the case, remove it, or change it to accomodate these other possibilities.

-- Abel --
0
 

Author Closing Comment

by:JaimeJegonia
ID: 31581272
Abel, thanks for quick solution and excellent tips.
0
 
LVL 39

Expert Comment

by:abel
ID: 24382454
you're welcome, always glad to be of help :)
0

Featured Post

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

695 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