0

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.

2
  • re.match anchors 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... use re.search and remove the $ and it should work (once you add in the B in the text your pattern will fail on...) Commented Jul 10, 2017 at 16:57
  • You could write this as: 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...) Commented Jul 10, 2017 at 17:03

3 Answers 3

2

It may be just a typo in your question post but i think you meant:

values = ["T12B45F8", "T12B67F8", "T12B34F8"]

then, just change this line:

parsedVal = re.match('T12B[0-9]{2}F8', checkVals[i])

to this one:

parsedVal = re.search('T12B[0-9]{2}F8', checkVals[i]).group()

this will give you the actual parts you're matching.

output:

True
True
True
False

as a conclusion, the entire code should look like this:

import re

values = ["T12B45F8", "T12B67F8", "T12B34F8"]

checkVals = ["rfgT12B45F8asd", "b65dT12B67F8lgkt", "4fgy7tgT12B34F8", "fgtrfT12B94F8fkg"]

for i in range(len(checkVals)):
    match = False
    parsedVal = re.search('T12B[0-9]{2}F8', checkVals[i]).group()
    for j in range(len(values)):
        if parsedVal == values[j]:
            match = True
    print(match)

I believe this is what you're looking for.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much. It's working as intended now :)
0

I think there is 2 problems

First:

The $ at the end of the regex is not allowing any characters after

Second:

Should be searching for T12B not T12

*It is also possible you made a type while entering your test data. For example rfgT12B45F8asd will not match your current expression because it contains T12B as a prefix to your numbers not T12

Final Regex:

T12B[0-9]{2}F8

2 Comments

Yeah sorry, that was a bad typo on my end. However I made that change to the regex and I am still only getting False values for my output.
@Skitzafreak I think parsedVal should be a boolean value depending on if the regex found a match. Use that to check if there was a match.
-1

Remove $ from your regexp, it matches at the end of string, while your strings don't end in F8. Also, use re.search instead of re.match.

1 Comment

Made a quick change to my code as per your suggestion, still getting all False results. My Regex was changed to T12B[0-9]{2}F8 and i changed re.match to re.search`

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.