I would like to add a regex to this code that will allow me to find that the reference ends with -FT(NUMBER) for example CHB-16236-FT-FT045 and increment this reference to have CHB-16236-FT046
import re
pattern_poteaux = r"(POT|PHT)+[-]+[0-9]{5}[-]+[a-zA-Z]{2}[-]+\d+$"
pattern_chambre = r"CHB+[-]+[0-9]+[-]+[a-zA-Z]{2}[-]+\d+$"
old_references = []
new_references = []
invalid_references = []
def attribute_check(pattern, sample_str):
"""
@param: regex pattern, sample string
return : True if string match regex pattern, False if not.
"""
sample_str = str(sample_str)
if re.search(pattern, sample_str) is not None:
return True
else:
return False
def increment_ref(pattern, sample_str):
"""
@param: string
return : incrément référence with 1
"""
if attribute_check(pattern, sample_str) == True:
old_references.append(sample_str)
return re.sub(r'[^-]+[0-9]$', lambda x: str(int(x.group()) + 1).zfill(len(x.group())), sample_str)
else:
invalid_references.append(sample_str)
if __name__ == "__main__":
reference_chamber = 'CHB-16236-FT-FT045'
# TODO increment reference with FT001 at the end
increment_ref(pattern_chambre, reference_chamber)
+'s inpattern_chambrefor?+at the end; what are the other+'s for?