0

I am inputting a text file (Line)(all strings). I am trying to make card_type to be true so it can enter the if statement, however, it never enters the IF statement. The output that comes out from the print line is:

imm48-1gb-sfp/imm48-1gb-sfp
imm-2pac-fp3/imm-2pac-fp3
imm5-10gb-xfp/imm5-10gb-xfp
sfm4-12/sfm4-12

This is the code:

    print str(card_type)            
    if card_type == re.match(r'(.*)/(.*)',line):
       card_type = card_type.group(1)

2 Answers 2

2

re.match will return a MatchObject if there's a match or None if there wasn't. Following code will capture the part before / character:

import re

line = 'imm48-1gb-sfp/imm48-1gb-sfp'
match = re.match(r'(.*?)/', line)
if match:
    card_type = match.group(1)
    print card_type
Sign up to request clarification or add additional context in comments.

1 Comment

True, I've change the example to use non-greedy pattern .*? which will work in case input has multiple / characters.
1

Card_type is a string. re.match() returns a Boolean (true or false, wether the regex matches the string or not).

Since they are different types, they cannot be equal and the if condition will not be fulfilled.

1 Comment

According to Python documentation re.match will return a MatchObject

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.