• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 292
  • Last Modified:

Strip parenthesis and contents

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.  

  • 4
2 Solutions
Try replace function. Replace with blank string. You can also use Substring function.

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

Sorry, Aanvik, missed your post.
7 new features that'll make your work life better

It’s our mission to create a product that solves the huge challenges you face at work every day. In case you missed it, here are 7 delightful things we've added recently to monday to make it even more awesome.

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
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 --
JaimeJegoniaAuthor Commented:
Abel, thanks for quick solution and excellent tips.
you're welcome, always glad to be of help :)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now