<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Arcpy: How to use Regular Expression with &amp;quot;Select by Attribute&amp;quot; in ArcGIS Pro Questions</title>
    <link>https://community.esri.com/t5/arcgis-pro-questions/arcpy-how-to-use-regular-expression-with-quot/m-p/1355615#M75754</link>
    <description>&lt;P&gt;Yes you are right, I just had to adjust the code a little bit. I also added a new field for all Values that follow the RegExp which I can use for my selection. Here is my code that worked with a specific Featur-Class:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# Import modules
import arcpy
import re

# Add a new field
arcpy.management.AddField(feature_class, 
                          "RegExp", 
                          "LONG")

# Define RegExpr
exp = '[A] \d+'

# Use an Update Cursor to Update all values in "RegExp" that follow the scheme
with arcpy.da.UpdateCursor(feature_class, ["OBJECTID", "name", "RegExp"]) as cursor:
    for row in cursor:
        oid = row[0]
        text = row[1]
        mo = re.search(exp, text)
        if mo is not None:
            # Update the value to 1 if condition is fullfilled
            row[2] = "1"
            cursor.updateRow(row)
            
query = ' ("RegExp" = 1)'

arcpy.management.SelectLayerByAttribute(feature_class, 
                                        "NEW_SELECTION", 
                                        query)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you really much for your help!&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 01 Dec 2023 12:19:40 GMT</pubDate>
    <dc:creator>MikeMoehlmann</dc:creator>
    <dc:date>2023-12-01T12:19:40Z</dc:date>
    <item>
      <title>Arcpy: How to use Regular Expression with "Select by Attribute"</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/arcpy-how-to-use-regular-expression-with-quot/m-p/1355581#M75744</link>
      <description>&lt;DIV class=""&gt;&lt;DIV class=""&gt;I want to select featurs from a Feature Class by using "arcpy.management.SelectLayerByAttribute" with a built-in Regular Expression.&lt;/DIV&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class=""&gt;The expression should follow this scema: Select all values in the attribute "name" starting with an "A" letter which is followed by a space " " which is followed by a random number series, e. g. "A 213" or "A 1". All other values starting with an "A" should not be selected, e. g. "A Court" or "Anson Street".&lt;/DIV&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/DIV&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;P&gt;I tried many ways, including using the "re" modul, but nothing worked. Here is one easy code example that didn't work:&lt;/P&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# SQL-Query with RegExpr
sql_expression = "name LIKE 'A %' AND regexp_like(SUBSTRING(name, 3, 1), '^[0-9]$')"

# Slect
arcpy.management.SelectLayerByAttribute(feature_class_path, "NEW_SELECTION", sql_expression)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance for any help&lt;/P&gt;</description>
      <pubDate>Fri, 01 Dec 2023 09:12:37 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/arcpy-how-to-use-regular-expression-with-quot/m-p/1355581#M75744</guid>
      <dc:creator>MikeMoehlmann</dc:creator>
      <dc:date>2023-12-01T09:12:37Z</dc:date>
    </item>
    <item>
      <title>Re: Arcpy: How to use Regular Expression with "Select by Attribute"</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/arcpy-how-to-use-regular-expression-with-quot/m-p/1355598#M75748</link>
      <description>&lt;P&gt;I don't believe the variant of SQL used in ArcPro supports regular expressions in the Select by Attribute tool.&amp;nbsp; You can still use regular expressions but you need to step through your data with a search cursor, then update the selection set.&amp;nbsp; I provide the code below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# Import modules
import arcpy
import re

# Initialise objects
layername = "p3"
mapname = "ORN"
fields = ["OBJECTID", "name"]
oids = list()
exp = '[A] \d+'

# Get layer object
p = arcpy.mp.ArcGISProject('current')
m = p.listMaps(mapname)[0]
l = m.listLayers(layername)[0]

# Step through layer using regexp to test text in field
with arcpy.da.SearchCursor(l, fields) as cursor:
    for row in cursor:
        oid = row[0]
        text = row[1]
        mo = re.search(exp, text)
        if mo is not None:
            oids.append(oid)

# Set selection set for layer object
l.setSelectionSet(oids, "NEW")&lt;/LI-CODE&gt;</description>
      <pubDate>Fri, 01 Dec 2023 10:48:44 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/arcpy-how-to-use-regular-expression-with-quot/m-p/1355598#M75748</guid>
      <dc:creator>DuncanHornby</dc:creator>
      <dc:date>2023-12-01T10:48:44Z</dc:date>
    </item>
    <item>
      <title>Re: Arcpy: How to use Regular Expression with "Select by Attribute"</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/arcpy-how-to-use-regular-expression-with-quot/m-p/1355602#M75749</link>
      <description>&lt;P&gt;Thank you for your answer. But this gives me the Error:&lt;/P&gt;&lt;PRE&gt;&lt;FONT color="#000000"&gt;Error: list index out of range&lt;BR /&gt;&lt;BR /&gt;&lt;/FONT&gt;&lt;/PRE&gt;&lt;P&gt;Also, do I really have to&amp;nbsp; define the layername and mapname to do this? Isn't there a way to define my specific feature class and use the function "arcpy.management.SelectLayerByAttribute" with the RegExpr?&lt;/P&gt;</description>
      <pubDate>Fri, 01 Dec 2023 11:11:48 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/arcpy-how-to-use-regular-expression-with-quot/m-p/1355602#M75749</guid>
      <dc:creator>MikeMoehlmann</dc:creator>
      <dc:date>2023-12-01T11:11:48Z</dc:date>
    </item>
    <item>
      <title>Re: Arcpy: How to use Regular Expression with "Select by Attribute"</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/arcpy-how-to-use-regular-expression-with-quot/m-p/1355611#M75752</link>
      <description>&lt;P&gt;You can do what you want, I have just tried to provide you with clear instructions so you get the overall logic of what you need to do. How you access your layers is up to you. The index error is likely because you have failed to provide the correct map\layer names?&lt;/P&gt;</description>
      <pubDate>Fri, 01 Dec 2023 11:29:32 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/arcpy-how-to-use-regular-expression-with-quot/m-p/1355611#M75752</guid>
      <dc:creator>DuncanHornby</dc:creator>
      <dc:date>2023-12-01T11:29:32Z</dc:date>
    </item>
    <item>
      <title>Re: Arcpy: How to use Regular Expression with "Select by Attribute"</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/arcpy-how-to-use-regular-expression-with-quot/m-p/1355615#M75754</link>
      <description>&lt;P&gt;Yes you are right, I just had to adjust the code a little bit. I also added a new field for all Values that follow the RegExp which I can use for my selection. Here is my code that worked with a specific Featur-Class:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# Import modules
import arcpy
import re

# Add a new field
arcpy.management.AddField(feature_class, 
                          "RegExp", 
                          "LONG")

# Define RegExpr
exp = '[A] \d+'

# Use an Update Cursor to Update all values in "RegExp" that follow the scheme
with arcpy.da.UpdateCursor(feature_class, ["OBJECTID", "name", "RegExp"]) as cursor:
    for row in cursor:
        oid = row[0]
        text = row[1]
        mo = re.search(exp, text)
        if mo is not None:
            # Update the value to 1 if condition is fullfilled
            row[2] = "1"
            cursor.updateRow(row)
            
query = ' ("RegExp" = 1)'

arcpy.management.SelectLayerByAttribute(feature_class, 
                                        "NEW_SELECTION", 
                                        query)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you really much for your help!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 01 Dec 2023 12:19:40 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/arcpy-how-to-use-regular-expression-with-quot/m-p/1355615#M75754</guid>
      <dc:creator>MikeMoehlmann</dc:creator>
      <dc:date>2023-12-01T12:19:40Z</dc:date>
    </item>
    <item>
      <title>Re: Arcpy: How to use Regular Expression with "Select by Attribute"</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/arcpy-how-to-use-regular-expression-with-quot/m-p/1355707#M75772</link>
      <description>&lt;P&gt;Although ArcGIS Pro itself doesn't support a standardized regular expression implementation across all data sources, nearly all data sources do support a flavor of it.&amp;nbsp; Without knowing the workspace or data source type, e.g., file geodatabase, mobile geodatabase, etc...., I can't offer any more specifics.&lt;/P&gt;</description>
      <pubDate>Fri, 01 Dec 2023 15:17:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/arcpy-how-to-use-regular-expression-with-quot/m-p/1355707#M75772</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2023-12-01T15:17:22Z</dc:date>
    </item>
  </channel>
</rss>

