Hi,
I'm beginner in ArcGIS Pro (I just switched from QGIS) and have some problem with some a little more advanced operation in field calculator. I would like to know how to use REGEX in field calculator to extract particular part of the string.
The string if in "Adr_for" field and in field "Number" I want to have extracted part.
Examples of the strings in "Adr_for":
13-0222222-1-02226-249A - -
13-022222-1-02225-33 - -
Expected result in "Number":
249A
33
Regex expression (which worked in QGIS):
((\d+(?!(\d|-))\w)|(\d+(?!(\d|-))))
I tried to use something like this in ArcGIS PRO field calculator but it didn't work.
Expression:
regex(!Adr_for!)
Code Block:
import re
def regex(Adr_for):
return re.search(r"""((\d+(?!(\d|-))\w)|(\d+(?!(\d|-))))""", Adr_for)
How should I do this to achieve expected result?
I presume your example is conincidently too simple to expect
!YourField!.split('-')[4]
to work then?
It's very nice solution but not perfect. It leaves empty spaces in field after values '33 ' and '249A ' (because between value and '-' there is space). I know I can fix it with some other basic function by second calculation pass but I would like to achieve that with one pass using regex, because it's something I used very often in QGIS.
BTW Is there any documents with description of all basic functions (like .split) of Field Calculator?
For the doc. on a few examples of Calculate Field, you could check Calculate Field examples—Data Management toolbox | ArcGIS Desktop
enclose within strip
(!YourField!.split('-')[4]).strip()
It will work, I suspect all the raw string triple quoting is doing something, just in the Python window all is good:
import re
p = re.compile('((\d+(?!(\d|-))\w)|(\d+(?!(\d|-))))')
Adr_for = '13-0222222-1-02226-249A - -'
re.search(p,Adr_for)
<_sre.SRE_Match object; span=(19, 23), match='249A'>