I want to select features from a shapefile where the value of column matches a regex then save only the selected features in a new shapefile.(I used regex101.com to write the regex and check if it's captured in the test string. So the regex is good). Here's what I did:
def filterByRegex(shapefile, output):
# Read the shapefile
shpfile = gpd.read_file(shapefile)
p = re.compile(
ur'(^[\d]\shour\sparking\s[\d]am.[\d]pm\smon.fri\sexcept\sby\szone\s.*|^[\d]\shour\svisitor\slimit\s[\d]am.[\d]pm\smon.sat\sexcept\sby\szone\sk\spermit.*|^[\d]\shour\svisitor\sparking\s[\d]am.[\d]pm\smon.fri\sexcept\sby\szone.*|^visitor\sparking\stime\slimits\s[\d][\d]\smin\s.\s\[chart\]\s.\sexcept\sby\szone.*|^stickered\s-\svisitor\sparking\stime\slimits\s.\s\[chart\]\s.\sexcept\sby\szone\s[A-Z].*|^Sign.\s[A-Z][\d][\d][\d][\d].\sReserved\sfor\scarpools\swith\spermit\s[\d]am-[\d][\d]am\smon-fri.*|^Sign.\s[A-Z][\d][\d][\d][\d].\s[\d]\shour\sparking\s[\d]am-[\d]pm\smon-sat\sexcept\sby\szone\s.*|^Sign.\s[A-Z]444[\d].\sparking.\srectangle.\sgreen.white.\s.*)', re.MULTILINE | re.IGNORECASE | re.VERBOSE)
test_str = shpfile['Comments']
print test_str
res = re.findall(p, str(test_str))
print res #empty geodataframe?
# Save the selected features to a new shapefile
res.to_file(output)
filterByRegex('./Outputs/joinedInput.shp', './Outputs/output.shp')
When I run the script I get an empty geodataframe :
Name: Comments, dtype: object
[]
Process finished with exit code 0
I tried using shpfile.Comments.str.extract() and shpfile.Comments.str.contains(), but I get the same result.
Any suggestions?