Remove strings within brackets
Forums
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
So, salman, if this is your
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.
FWIW, here's the code.
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
Brian - That worked a treat,
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.
Instead of trying to remove
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.