Python Forum

Full Version: Regex: finding if three groups have a value in them
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello, I have started learning python via documentation & videos because I trying to make a program that converts episode number and timestamps in a .txt to an XML chapter file.

Write now I am trying to take the raw episode raw format and output 1 or 2 numbers and groups 6 is giving me There was no Match when it should be saying 6 is a match. I don't know if I`m going about this the right way, so any Ideas or suggestions would be great.
import re
Input = 'd7 23'
###Input should output 23 via groups 2 or 3 or 6 

##Input = '23'
##Input = 'ep23'
ep_regex = '((?i)EP(\d\d?))?(^\d\d?$)?(((?i)d\w[\s|:])?(\d\d?))?'
ep_prog = re.compile(ep_regex) 
ep_result = ep_prog.match(Input)

one = ep_result.group(1)
two = ep_result.group(2)
three = ep_result.group(3)
four = ep_result.group(4)
five = ep_result.group(5)
six = ep_result.group(6)

if two or three or six is True:
    if two == None:
        print('No match in G2')
        pass
    else:
        print('2 is a Match!')
    if three == None:
        print('no match in G3')
        pass
    else:
        print('3 is a Match!')
    if six == None:
        print('No Match in G6')
        pass
    else:
        print ("6 is a match!")        
else:
    print("There was no Match")
print('group 1 -->',one)
print('group 2 -->',two)
print('group 3 -->',three)
print('group 4 -->',four)
print('group 5 -->',five)
print('group 6 -->',six)
Here`s the error that I WAS getting if that helps.
Error:
Warning(from warnings module): File "C:\Users\new user\Desktop\py read & replace\Regex_Examples\ep_re_group_match.py", line 9 ep_prog = re.compile(ep_regex) DeprecationWarning: Flags not at the start of the expression '((?i)EP(\\d\\d?))?(^\\d' (truncated)
The depreciation warning is for using "?i" instead of "i?"
ep_regex = '((i?)EP(\d\d?))?(^\d\d?$)?(((i?)d\w[\s|:])?(\d\d?))?'
look at this line
if two or three or six is True:
then read
https://python-forum.io/Thread-Multiple-...or-keyword


two and three are None and six is a str
so your line is equivalent to
if None or None or ('23' is True):
which is same as
if False or False or False:
https://docs.python.org/3/library/stdtypes.html#truth-value-testing

I guess you probably you want to do
if two or three or six:
if you want to to check that at least of them is not None (relying on Truth value of non-empty string)

there are other possible implementations for this check
Thanks Brian and Deanhystad for the help I did not know it was that simple.

Here`s the fixed code for the people surfing on the forms ;)
import re
Input = 'd7 23'
###Input should output 23 via groups 2,3,6 
##Input = '23'
##Input = 'ep23'
ep_regex = '((i?)EP(\d\d?))?(^\d\d?$)?(((i?)d\w[\s|:])?(\d\d?))?'
ep_prog = re.compile(ep_regex) 
ep_result = ep_prog.match(Input)

#zero = (ep_result.group(0))
one = ep_result.group(1)
two = ep_result.group(2)
three = ep_result.group(3)
four = ep_result.group(4)
five = ep_result.group(5)
six = ep_result.group(6)

if two or three or six:
    if two == None:
        pass
    else:
        print('Your Episode number is: %s' % (two))
    if three == None:
        pass
    else:
        print('Your Episode number is: %s' % (three))
    if six == None:
        pass
    else:
        print('Your Episode number is: %s' % (six))
else:
    print("There was no Match")
it's recommended to use is (identity operator), not == (equality) with None, e.g.
if two is None: 
PEP8 Programming Recommendations:
Quote:Comparisons to singletons like None should always be done with is or is not, never the equality operators.
When I wrote this didn’t understand the
“is” operator looks to see the if what your comparing is the same in memory.
It compares the object ID which is the memory location of the object.

In earlier versions of Python you could do nasty things like change the value of True and False. Nobody raised a peep if you wrote:
x = True
True = 1
if x != True:
    print x, '!=', True
However if you wrote his code using is
x = True
True = 1
if x is not True:
    print(x, '!=', True)
The output of the first example is indeterminant. Maybe True was set to 1. The second example never prints because no matter what value we assign True, it always "is" True. It is comforting to write code that cannot be broken.

"is" is slightly more efficient. When testing "x == True" we need to call __eq__(x, True which has to get the value in x and the value in True and do a comparison. When using "x is True" we can directly compare the object ID's.

"Comparisons to singletons like None should always be done with is or is not, never the equality operators."

"never" is a bad word. Many small integers are singletons. I don't remember the exact range, some integers are singletons:
x = 1
print('x is 1', x is 1)
x = 1000
print('x is 1000', x is 1000)
Output:
Warning (from warnings module): File "C:\Users\djhys\Documents\Python\Musings\junk.py", line 4 print('x is 1000', x is 1000) SyntaxWarning: "is" with a literal. Did you mean "=="? >>> =========== RESTART: C:\Users\djhys\Documents\Python\Musings\junk.py =========== x is 1 True x is 1000 True
At least they give you a warning.

But the message in that message is a good one. When you can use it, "is" is a better test than "==", and both are bar better than the implicit test in "if x:" If "x" what? If x != 0 and x != None and x != False?
Thanks for the detailed response, I’m a trying to learn as I go, an making use of all this free time To learn Python.