Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import re
- RES_STRING='MINERAL CLAIM'
- REGEX_HAYSTACK='DISTRICT LOT 5639, BEING AWARD NO. 2 MINERAL CLAIM, KDYD'
- REGEX_STRING=re.compile(RES_STRING)
- print "searching for "+RES_STRING+" in "+REGEX_HAYSTACK
- REGEX_MATCH = REGEX_STRING.search(REGEX_HAYSTACK)
- if REGEX_MATCH:
- print "found '"+REGEX_MATCH.group()+"'"
- else:
- print "No match found"
- def select_by_regex(input_layer,attribute_name,regex_string):
- import re
- RES_STRING=regex_string
- attribute_name_idx = input_layer.fieldNameIndex(attribute_name)
- if attribute_name_idx<0:
- raise valueError("cannot find attribute"+attribute_name)
- else:
- fids=[]
- for feature in input_layer.getFeatures():
- REGEX_HAYSTACK=feature[attribute_name_idx]
- REGEX_STRING=re.compile(RES_STRING)
- REGEX_MATCH = REGEX_STRING.search(REGEX_HAYSTACK)
- if REGEX_MATCH:
- fids.append(feature.id())
- else:
- pass
- input_layer.setSelectedFeatures(fids)
- #USAGE BIT
- input_layer = QgsVectorLayer('path/to/shape/file.shp','layer name', 'ogr')
- QgsMapLayerRegistry.instance().addMapLayer(input_layer)
- regex_string='MINERAL CLAIM'
- attribute_name='TITLE'
- select_by_regex(input_layer,attribute_name,regex_string)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement