So I am creating a program which reads input Strings, and sees if they contains codes within a list. I am attempting to use a regex to get the matching string, but am having a bit of a problem with my regex. Here is my code for reference:
import re
values = ["T1245F8", "T1267F8", "T1234F8"]
checkVals = ["rfgT12B45F8asd", "b65dT12B67F8lgkt", "4fgy7tgT12B34F8", "fgtrfT12B94F8fkg"]
for i in range(len(checkVals)):
match = False
parsedVal = re.match('T12B[0-9]{2}F8', checkVals[i])
for j in range(len(values)):
if parsedVal == values[j]:
match = True
print(match)
The output I am expecting if 3 True and 1 False statement printed out. However instead of get 4 False statements.
EDIT: Fixed a typo in my regex, but it still isn't working.
re.matchanchors the search to the start of the string and the$is your regex is anchoring to the end... so you're only going to match strings that are identical to the pattern... usere.searchand remove the$and it should work (once you add in theBin the text your pattern will fail on...)matches = [val for val in checkVals if re.search(r'T12B\d{2}F8', val)]... (which matches all 4 - so not sure which one you're expecting to not match...)