Solved

Strip parenthesis and contents

Posted on 2009-05-13
6
280 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Visual Studio editor? 1 38
SlingPlayer alternatives 1 31
Unable to upgrade to .Net 4.6.2 on Server 2012r2 2 42
C# HTTP GET method sample code 3 40
Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

910 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

23 Experts available now in Live!

Get 1:1 Help Now