Keeping only 7 digit number within field name using ArcGIS Pro field calculator and regexDebugging error 000539 from ArcGIS Field Calculator?Error when calculate field using VBscript or Python (slice a field)Adding a sequential number using ArcGIS field calculator to string file and keeping the 0 in front of the number?Extracting text within parenthesis in a QGIS attribute tableHow to get around “The field is not nullable” error?Field Calculator: Split address into house number, street nameError - Not defined - ArcpyWriting If-Then statement in ArcGIS Pro field calculator using Python?ArcGIS Pro Field Calculator completing without error, but no change to Feature?Using Python Parser in ArcGIS Pro Field Calculator?
Can I change the license of a forked project to the MIT if the license of the parent project has changed from the GPL to the MIT?
Why does this RX-X lock not appear in Extended Events?
Does Wolfram Mathworld make a mistake describing a discrete probability distribution with a probability density function?
What are the cons of stateless password generators?
How did the Axis intend to hold the Caucasus?
Why isn't there any 9.5 digit multimeter or higher?
What do you call a flexible diving platform?
How can I kill my goat?
How could Nomadic scholars effectively memorize libraries worth of information
Exploiting the delay when a festival ticket is scanned
What are the closest international airports in different countries?
Is there a way to know the composition of a Team GO Rocket before going into the fight?
How did the SysRq key get onto modern keyboards if it's rarely used?
Should I accept an invitation to give a talk from someone who might review my proposal?
Should I bike or drive to work? (6.8 mi)
What happens when a flying sword is killed?
Why would anyone ever invest in a cash-only etf?
Rampant sharing of authorship among colleagues in the name of "collaboration". Is not taking part in it a death knell for a future in academia?
Self-deportation of American Citizens from US
Finding out if upgrading to a newer macOS version will cause issues?
8086 stack segment and avoiding overflow in interrupts
Why is it "on the inside" and not "in the inside"?
How do I use JSON.generator to generate an unnamed array?
Did Vladimir Lenin have a cat?
Keeping only 7 digit number within field name using ArcGIS Pro field calculator and regex
Debugging error 000539 from ArcGIS Field Calculator?Error when calculate field using VBscript or Python (slice a field)Adding a sequential number using ArcGIS field calculator to string file and keeping the 0 in front of the number?Extracting text within parenthesis in a QGIS attribute tableHow to get around “The field is not nullable” error?Field Calculator: Split address into house number, street nameError - Not defined - ArcpyWriting If-Then statement in ArcGIS Pro field calculator using Python?ArcGIS Pro Field Calculator completing without error, but no change to Feature?Using Python Parser in ArcGIS Pro Field Calculator?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I have a field titled GEOGRAPHY
, which contains hundreds of records containing various strings of all similar format, for example:
"Witless Bay (1001559) T 00000 ( 6.5%)"
"Laniel (2485905) NO 00909 ( 6.7%)"
"Contrecoeur (2459035) V 00000 ( 4.9%)"
However, in order for me to join this table with my boundary layer, I need the records in this field to only contain the 7 digit number in the parenthesis, i.e. 1001559
, for the Witless Bay
record.
I cannot figure out how to do this through field calculator using regex. I have looked at ESRI's examples, and they only show examples for substitutions. Using LEFT()
or RIGHT()
doesn't work either, as the string lengths are variable.
I have tried something to the extent of the following without success with attached error:
Expression Type: Python 3
Expression:
GEOGRAPHY = update_name(!GEOGRAPHY!)
Code Block:
import re
def update_name(geo_name):
return re.search(r"""((.*?))""", geo_name)
I do not understand why this wouldn't work. The expression is valid ((.*?))
. It gives me the following error:
The field is not nullable. [GEOGRAPHY]
Failed to execute (CalculateField).
I understand that it gives that error because it can't return a NULL value, but should this not be returning the 7 digit number?
field-calculator arcgis-pro python-parser regex regular-expression
New contributor
hhart4 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
I have a field titled GEOGRAPHY
, which contains hundreds of records containing various strings of all similar format, for example:
"Witless Bay (1001559) T 00000 ( 6.5%)"
"Laniel (2485905) NO 00909 ( 6.7%)"
"Contrecoeur (2459035) V 00000 ( 4.9%)"
However, in order for me to join this table with my boundary layer, I need the records in this field to only contain the 7 digit number in the parenthesis, i.e. 1001559
, for the Witless Bay
record.
I cannot figure out how to do this through field calculator using regex. I have looked at ESRI's examples, and they only show examples for substitutions. Using LEFT()
or RIGHT()
doesn't work either, as the string lengths are variable.
I have tried something to the extent of the following without success with attached error:
Expression Type: Python 3
Expression:
GEOGRAPHY = update_name(!GEOGRAPHY!)
Code Block:
import re
def update_name(geo_name):
return re.search(r"""((.*?))""", geo_name)
I do not understand why this wouldn't work. The expression is valid ((.*?))
. It gives me the following error:
The field is not nullable. [GEOGRAPHY]
Failed to execute (CalculateField).
I understand that it gives that error because it can't return a NULL value, but should this not be returning the 7 digit number?
field-calculator arcgis-pro python-parser regex regular-expression
New contributor
hhart4 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
I have a field titled GEOGRAPHY
, which contains hundreds of records containing various strings of all similar format, for example:
"Witless Bay (1001559) T 00000 ( 6.5%)"
"Laniel (2485905) NO 00909 ( 6.7%)"
"Contrecoeur (2459035) V 00000 ( 4.9%)"
However, in order for me to join this table with my boundary layer, I need the records in this field to only contain the 7 digit number in the parenthesis, i.e. 1001559
, for the Witless Bay
record.
I cannot figure out how to do this through field calculator using regex. I have looked at ESRI's examples, and they only show examples for substitutions. Using LEFT()
or RIGHT()
doesn't work either, as the string lengths are variable.
I have tried something to the extent of the following without success with attached error:
Expression Type: Python 3
Expression:
GEOGRAPHY = update_name(!GEOGRAPHY!)
Code Block:
import re
def update_name(geo_name):
return re.search(r"""((.*?))""", geo_name)
I do not understand why this wouldn't work. The expression is valid ((.*?))
. It gives me the following error:
The field is not nullable. [GEOGRAPHY]
Failed to execute (CalculateField).
I understand that it gives that error because it can't return a NULL value, but should this not be returning the 7 digit number?
field-calculator arcgis-pro python-parser regex regular-expression
New contributor
hhart4 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
I have a field titled GEOGRAPHY
, which contains hundreds of records containing various strings of all similar format, for example:
"Witless Bay (1001559) T 00000 ( 6.5%)"
"Laniel (2485905) NO 00909 ( 6.7%)"
"Contrecoeur (2459035) V 00000 ( 4.9%)"
However, in order for me to join this table with my boundary layer, I need the records in this field to only contain the 7 digit number in the parenthesis, i.e. 1001559
, for the Witless Bay
record.
I cannot figure out how to do this through field calculator using regex. I have looked at ESRI's examples, and they only show examples for substitutions. Using LEFT()
or RIGHT()
doesn't work either, as the string lengths are variable.
I have tried something to the extent of the following without success with attached error:
Expression Type: Python 3
Expression:
GEOGRAPHY = update_name(!GEOGRAPHY!)
Code Block:
import re
def update_name(geo_name):
return re.search(r"""((.*?))""", geo_name)
I do not understand why this wouldn't work. The expression is valid ((.*?))
. It gives me the following error:
The field is not nullable. [GEOGRAPHY]
Failed to execute (CalculateField).
I understand that it gives that error because it can't return a NULL value, but should this not be returning the 7 digit number?
field-calculator arcgis-pro python-parser regex regular-expression
field-calculator arcgis-pro python-parser regex regular-expression
New contributor
hhart4 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
hhart4 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited 8 hours ago
PolyGeo♦
54.8k17 gold badges86 silver badges258 bronze badges
54.8k17 gold badges86 silver badges258 bronze badges
New contributor
hhart4 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked 8 hours ago


hhart4hhart4
132 bronze badges
132 bronze badges
New contributor
hhart4 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
hhart4 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Open up a Python console and run the following line:
re.search(r"""((.*?))""", "Witless Bay (1001559) T 00000 ( 6.5%)")
You will see it returns a match object (<_sre.SRE_Match object at 0x15AB2E60>
) as @mikewatt commented.
There is no way ArcGIS can insert this object inside a field. You have to use the group
method.
Following the preceding example, you can access the groups depending on your result. In this case you can get only two groups and (I assume) you are interested in the second one:
match = re.search(r"""((.*?))""", "Witless Bay (1001559) T 00000 ( 6.5%)")
print(match.group(0)) # '(1001559)'
print(match.group(1)) # '1001559'
print(match.group(2)) # IndexError: no such group
Furthermore, I would add some logic to handle the cases where re.search
does not find anything and returns None
.
For example, if you run re.search(r"""((.*?))""", "Witless Bay")
it will return None
and if you run re.search(r"""((.*?))""", "Witless Bay").group(0)
you will get the following error:
AttributeError: 'NoneType' object has no attribute 'group'
Your code block could look something similar to this:
import re
def update_name(geo_name):
match = re.search(r"""((.*?))""", geo_name)
if match:
return match.group(1)
else:
return '-99999' # an arbitrary value
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "79"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
hhart4 is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f330415%2fkeeping-only-7-digit-number-within-field-name-using-arcgis-pro-field-calculator%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Open up a Python console and run the following line:
re.search(r"""((.*?))""", "Witless Bay (1001559) T 00000 ( 6.5%)")
You will see it returns a match object (<_sre.SRE_Match object at 0x15AB2E60>
) as @mikewatt commented.
There is no way ArcGIS can insert this object inside a field. You have to use the group
method.
Following the preceding example, you can access the groups depending on your result. In this case you can get only two groups and (I assume) you are interested in the second one:
match = re.search(r"""((.*?))""", "Witless Bay (1001559) T 00000 ( 6.5%)")
print(match.group(0)) # '(1001559)'
print(match.group(1)) # '1001559'
print(match.group(2)) # IndexError: no such group
Furthermore, I would add some logic to handle the cases where re.search
does not find anything and returns None
.
For example, if you run re.search(r"""((.*?))""", "Witless Bay")
it will return None
and if you run re.search(r"""((.*?))""", "Witless Bay").group(0)
you will get the following error:
AttributeError: 'NoneType' object has no attribute 'group'
Your code block could look something similar to this:
import re
def update_name(geo_name):
match = re.search(r"""((.*?))""", geo_name)
if match:
return match.group(1)
else:
return '-99999' # an arbitrary value
add a comment |
Open up a Python console and run the following line:
re.search(r"""((.*?))""", "Witless Bay (1001559) T 00000 ( 6.5%)")
You will see it returns a match object (<_sre.SRE_Match object at 0x15AB2E60>
) as @mikewatt commented.
There is no way ArcGIS can insert this object inside a field. You have to use the group
method.
Following the preceding example, you can access the groups depending on your result. In this case you can get only two groups and (I assume) you are interested in the second one:
match = re.search(r"""((.*?))""", "Witless Bay (1001559) T 00000 ( 6.5%)")
print(match.group(0)) # '(1001559)'
print(match.group(1)) # '1001559'
print(match.group(2)) # IndexError: no such group
Furthermore, I would add some logic to handle the cases where re.search
does not find anything and returns None
.
For example, if you run re.search(r"""((.*?))""", "Witless Bay")
it will return None
and if you run re.search(r"""((.*?))""", "Witless Bay").group(0)
you will get the following error:
AttributeError: 'NoneType' object has no attribute 'group'
Your code block could look something similar to this:
import re
def update_name(geo_name):
match = re.search(r"""((.*?))""", geo_name)
if match:
return match.group(1)
else:
return '-99999' # an arbitrary value
add a comment |
Open up a Python console and run the following line:
re.search(r"""((.*?))""", "Witless Bay (1001559) T 00000 ( 6.5%)")
You will see it returns a match object (<_sre.SRE_Match object at 0x15AB2E60>
) as @mikewatt commented.
There is no way ArcGIS can insert this object inside a field. You have to use the group
method.
Following the preceding example, you can access the groups depending on your result. In this case you can get only two groups and (I assume) you are interested in the second one:
match = re.search(r"""((.*?))""", "Witless Bay (1001559) T 00000 ( 6.5%)")
print(match.group(0)) # '(1001559)'
print(match.group(1)) # '1001559'
print(match.group(2)) # IndexError: no such group
Furthermore, I would add some logic to handle the cases where re.search
does not find anything and returns None
.
For example, if you run re.search(r"""((.*?))""", "Witless Bay")
it will return None
and if you run re.search(r"""((.*?))""", "Witless Bay").group(0)
you will get the following error:
AttributeError: 'NoneType' object has no attribute 'group'
Your code block could look something similar to this:
import re
def update_name(geo_name):
match = re.search(r"""((.*?))""", geo_name)
if match:
return match.group(1)
else:
return '-99999' # an arbitrary value
Open up a Python console and run the following line:
re.search(r"""((.*?))""", "Witless Bay (1001559) T 00000 ( 6.5%)")
You will see it returns a match object (<_sre.SRE_Match object at 0x15AB2E60>
) as @mikewatt commented.
There is no way ArcGIS can insert this object inside a field. You have to use the group
method.
Following the preceding example, you can access the groups depending on your result. In this case you can get only two groups and (I assume) you are interested in the second one:
match = re.search(r"""((.*?))""", "Witless Bay (1001559) T 00000 ( 6.5%)")
print(match.group(0)) # '(1001559)'
print(match.group(1)) # '1001559'
print(match.group(2)) # IndexError: no such group
Furthermore, I would add some logic to handle the cases where re.search
does not find anything and returns None
.
For example, if you run re.search(r"""((.*?))""", "Witless Bay")
it will return None
and if you run re.search(r"""((.*?))""", "Witless Bay").group(0)
you will get the following error:
AttributeError: 'NoneType' object has no attribute 'group'
Your code block could look something similar to this:
import re
def update_name(geo_name):
match = re.search(r"""((.*?))""", geo_name)
if match:
return match.group(1)
else:
return '-99999' # an arbitrary value
edited 7 hours ago
PolyGeo♦
54.8k17 gold badges86 silver badges258 bronze badges
54.8k17 gold badges86 silver badges258 bronze badges
answered 8 hours ago


Marcelo VillaMarcelo Villa
2,2132 gold badges9 silver badges21 bronze badges
2,2132 gold badges9 silver badges21 bronze badges
add a comment |
add a comment |
hhart4 is a new contributor. Be nice, and check out our Code of Conduct.
hhart4 is a new contributor. Be nice, and check out our Code of Conduct.
hhart4 is a new contributor. Be nice, and check out our Code of Conduct.
hhart4 is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Geographic Information Systems Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f330415%2fkeeping-only-7-digit-number-within-field-name-using-arcgis-pro-field-calculator%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown