Solved

Strip parenthesis and contents

Posted on 2009-05-13
6
281 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
  • 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Code works but it's slow 28 70
SQL Help 27 45
parsing JSON help 1 9
Formula for calculating ROI on training 6 26
A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

777 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