Remove parentheses from data using field calculator.

3810
11
01-16-2018 06:32 AM
TerryMergl
New Contributor III

Good morning.

I am not real savvy with field calculator.  We have a couple fields that contain service address information.  Someone opted to add additional information enclosed by parentheses in this field.  I need to remove the parentheses and data within.  Any advice or suggestions appreciated.

Thanks

Terry

0 Kudos
11 Replies
RobertScheitlin__GISP
MVP Emeritus

Terry,

   Using VBScript in the field calculator you just use Mid([yourfieldname], 1, (InStr([yourfieldname],"(") - 1)) 

This will look in the thread for the first occurance of "(" and take everything before the "(" and use it for the field calc.

TerryMergl
New Contributor III

Thank you Robert.  I'll give it a shot.

0 Kudos
DanPatterson_Retired
MVP Emeritus

Terry a little function for the field calculator

Python parser of course

expression box   ....  brace(!YourFieldNameHere!)

code block

def brace(fld):
    """replace brackets"""
    a = ['[', ']', '{', '}', '(', ')']
    for i in a:
        fld = fld.replace(i, "")
    return fld

of course you are putting the result in a new text field of appropriate width

RobertScheitlin__GISP
MVP Emeritus

Dan,

  What about the data within?

I need to remove the parentheses and data within

0 Kudos
DanPatterson_Retired
MVP Emeritus

ahh missed that bit

TerryMergl
New Contributor III

Thank you Dan.

What happened is when the service address got populated, someone added additional info between the parentheses in the same field, rather than using an address2 field

I don't need to save that info, just remove it.

Thank you

0 Kudos
DanPatterson_Retired
MVP Emeritus

well the other function you might use.... but

Keep between the brackets

fld = "some stuff (the stuff) with more"

fld.split(")")[0].split("(")[1]

# yields

'the stuff'

# just replace fld with !YourFieldName!
‍‍‍‍‍‍‍‍‍

Dump between the brackets

fld = "some stuff (the stuff) with more"

fld.split("(")[0].strip() + fld.split(")")[1]

'some stuff with more'
JamesMacKay3
Occasional Contributor

Using the Python interpreter, import the regular expression module (re) in the pre-logic script code block (click on Show Codeblock to open this):

import re

Then use a substitution along the lines of this:

re.sub(r"\([^\)]+\)", "", !ServiceAddr!)

Should work fine with multiple pairs of parentheses but will likely need to be tweaked if you've got nested pairs.

DanPatterson_Retired
MVP Emeritus

you missed the bit I did earlier... the stuff between the braces needs to be removed as well

0 Kudos