Remove strings within brackets

9 posts / 0 new
Last post
salman.qasim
Offline
Joined: 07/03/2014 - 09:24
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's picture
Brian Element
Offline
Joined: 07/11/2012 - 19:57

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
Offline
Joined: 07/03/2014 - 09:24

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.

Images: 
Brian Element's picture
Brian Element
Offline
Joined: 07/11/2012 - 19:57

Hi Salman, I created a custom function that will remove the () and everything in between.  Maybe someone else can think of a direct way to do it with the equation editor.

Brian Element's picture
Brian Element
Offline
Joined: 07/11/2012 - 19:57

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
Offline
Joined: 08/05/2014 - 10:29

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
Offline
Joined: 08/05/2014 - 10:29

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
Offline
Joined: 08/05/2014 - 10:29

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
Offline
Joined: 07/03/2014 - 09:24

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.