Wednesday, 4 October 2017

VBscript Regular Expression in ArcMap Field Calculator


I'm looking for a regex in VBscript that I can use on a ArcMap table. I know what it would look like in python:


pat =r'[0-9]{3,4}'



This filters all numeric characters from the string which have the length 3 to 4. So from AO123 you'd get 123 and from VO2-324C, 324.


What would be the equivalent in VBScript? How do I implement it with the RexExp Object? ArcGIS has a Field calculator that let's you execute VBScript code. But even the most simple regex (see screenshot) fails with the error message object needed: 're'.


enter image description here



Answer



The error is because you have not initialized the RegExp Object using CreateObject. I am getting results with the below script.


Pre-Logic VBA Script Code


Set re = CreateObject("VBScript.RegExp")
With re
.Pattern = "[0-9]{3,4}"
.Global = False

.IgnoreCase = False
End With
targetString = [OUDE_NAAM] 'your field here

Final Result Variable


re.Execute(targetString).Item(0)

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...