Skip to main content

Remove strings within brackets

Hi,Hoping someone could help me with this. I am trying to figure out a way to remove all strings within brackets in a specified column so for example:Original data - Remove (Sep 09-10) strings (Oct 09-10) within (Nov 09-10) brackets (Dec 09-10).
Required data - Remove strings within brackets.I hope this makes sense and help would be much appreciated.Thanks,Salman

Brian Element Thu, 09/25/2014 - 07:18

Instead of trying to remove the date I instead removed the brackets using the following function:

@Remove(@Remove(FIELD, "("), ")")

This will remove the ( and ) from the field.  So you would create a virtual character field to hold the results.

salman.qasim Thu, 09/25/2014 - 09:33

Thanks for the quick reply Brian. That removes all the brackets but not the data contained within them. The data within the brackets may not always be a date but in every instance, will need to be removed. This is what happens with the code you have provided.

Brian Element Thu, 09/25/2014 - 12:53

Thanks now I understand what you are looking for.  I am not sure if you can do this with the @regexp function or not, unfortunately I am not an expert in that.  I could probably do a custom function that would remove all the items.

robgentile Thu, 09/25/2014 - 13:56

So, Salman, if this is your original data "Original data - Remove (Sep 09-10) strings (Oct 09-10) within (Nov 09-10) brackets (Dec 09-10)"
do you want your result to be "Original data - Remove () strings () within () brackets ()" ?
 
If so, an equation like this might help, if the number of bracket pairs is constant.
 
@Split( INPUT_FIELD , "", "(", 1) +"()" +@Split( INPUT_FIELD , ")", "(", 1) +"()" + @Split( INPUT_FIELD , ")", "(", 2) +"()"+ @Split( INPUT_FIELD , ")", "(", 3) +"()"
 
 
 
INPUT_FIELD = "Original data - Remove (Sep 09-10) strings (Oct 09-10) within (Nov 09-10) brackets (Dec 09-10)"
 
OUTPUT = "Original data - Remove () strings () within () brackets ()"
 
 
It first splits on a left "(", and then splits again, using the pair ")(" for more pieces. 
 
The only caution  I would give is that this assumes the same # of "( )" in each line.

robgentile Fri, 09/26/2014 - 11:06

Salman,
I played around and made a custom script that may do what you want.
It will work for any number of PAIRED parentheses. I couldn't upload it to the site, but if you give me your email I could send it on.
 
Rob

robgentile Fri, 09/26/2014 - 11:14

FWIW, here's the code. (Standard disclaimers apply).
 
Function EmptyParentheses(TheString As String) As String

'EmptyParentheses

' Desc: Accepts a string, then returns that string with any contents that appear within parentheses truncated.
' Function should return correct results for any # of Parens, but they must be all matched pairs AND in correct left/right order.

' Usage: EmptyParentheses("Original data - Remove (Sep 09-10) strings (Oct 09-10) within (Nov 09-10) brackets (Dec 09-10)")

' Input = "Original data - Remove (Sep 09-10) strings (Oct 09-10) within (Nov 09-10) brackets (Dec 09-10)"
' Output= "Original data - Remove () strings () within () brackets ()"

' Notes: This is a custom '#' IDEA function, used within the IDEA interface and stored in a file as "EmptyParentheses.ideafunc"

' Created: 2014/9/26
' Author: Rob Gentile
'
' Revisions: 2014/9/26 RMG Initial Script

Dim numLeftParens As Integer
Dim numRightParens As Integer
Dim numParensPairs As Integer
Dim ThisParensPair As Integer
Dim ParenthesesMatch As Boolean

'First, do the parentheses match? Return error if they do not.

numLeftParens = NumberOfCharsInString(TheString, "(")
numRightParens = NumberOfCharsInString(TheString, ")")
If numLeftParens = numRightParens Then
ParenthesesMatch = True
numParensPairs = numLeftParens
emptyparentheses = "Good = - the Parentheses are matched -- there are " & numLeftParens & "'" & "(" & "' , and there are " & numRightParens & " '" & ")" & "' ! "
Else
ParenthesesMatch = False
emptyparentheses = "Error - the Parentheses are not matched -- there are " & numLeftParens & "'" & "(" & "' , but there are " & numRightParens & " '" & ")" & "' ! "
End If

'So,OK, parentheses match, and we need to get the pieces we do want.

If ParenthesesMatch Then
emptyparentheses = "" 'reset variable
emptyparentheses = iSplit( TheString , "", "(", 1) +"(" 'grab initial text before first LEFT paren
For ThisParensPair = 1 To (numParensPairs - 1)
emptyparentheses = emptyparentheses +")" + iSplit( TheString , ")", "(", ThisParensPair) +"("
Next ProcessEachParensPairs
emptyparentheses = emptyparentheses + ")"
emptyparentheses = emptyparentheses + iSplit( TheString , "", ")", (ThisParensPair +1)) 'grab Last bit of text after last RIGHT paren
End If

'MsgBox EmptyParentheses

End Function

Function NumberOfCharsInString(TheString As String, TheChar As String) As Integer

Dim numLedftParens As Integer
Dim cnt As Integer
Dim StringLength As Integer
Dim CharInString As Integer
Dim ThisChar As String
Dim CharToCount As String

CharToCount = TheChar
cnt = 0
StringLength = Len(TheString)
For CharInString = 1 To StringLength
ThisChar = Mid( TheString, CharInString, 1)
If ThisChar = CharToCount Then cnt = cnt + 1
Next
NumberOfCharsInString = cnt

End Function


 

salman.qasim Wed, 10/01/2014 - 04:42

Brian - That worked a treat, exactly what I needed so thanks!Rob - Your code leaves the brackets behind whereas I need the whole string removed including the brackets and all the data within them. It was easy to remove the brackets there after so still works well.
Thanks again guys, your help is much appreciated.

The website encountered an unexpected error. Try again later.