I have a string field in the attribute table for a featureclass which has values like: "abc123_def23_987" and "fks243_ufs876_21". I've created another integer field and wish to extract the numbers after the last underscore (_) so that it yields the numbers "987" and "21" from the examples above respectively. As I'm using I'm using ArcGIS Pro. How can I accomplish this using the python in the "Calculate field" within ArcGIS Pro? I would greatly appreciate help on this. Thanks.
Solved! Go to Solution.
In the Calculate Field tool, you can use Regular Expressions.
I like regular expressions, a lot, but I also realize they aren't ideal for simple situations like the OP presented here. Since using split involves using built-in functionality, i.e., no need to import additional modules, and split is likely 3-4 times faster than regular expression in this case; I think using split is a better approach.
But if you try to extract only numbers in a string that hasn't any "splitting character", your suggestion doesn't work. So, regular expressions can be a very powerfull option. I ran this code today in a geodatabase with 10,000 polygons. It took 1 second to calculate.
There has to be some kind of splitting character or characters, otherwise there would be no way to know what differentiates one group of numbers in a string from another. In the OP's original example and data set, the strings happened to be very structured with a convenient splitting character right before the final group of numbers, which made using str.split simple. In your example, relying on the decimal digit regex special sequence implies the splitting character is any non-decimal digit, which presents its own challenges if the numbers included commas or periods.
As I mentioned above, I like and use regex all the time, but it can also be overkill when the situation can be handled with basic string methods.