Is there any way that regular expression match inside a if block would interfere with the code inside an else block?
In the following code, I have 100 test strings that are being inputed as job_name that should be appended to my del_job_ids array. Only 99 of them are being appended.
for j in jobs:
name = jobs[j]['Job_Name'][0]
p=re.compile("(\\w*):?\\w*-?\\d*")
if ((':' in name) or ('-' in name)):
name=p.match(name).group(1)
else:
#if name==job_name and state!='C':
#print "ok"
#del_job_ids.append(j)
pass
if name==job_name and state!='C':
del_job_ids.append(j)
name=None
If I comment out the line name=p.match(name).group(1), the one case that doesn’t get appended before now get appended. I can also append that job by uncommmenting the code in the else block (the string passes the conditon for getting appended).
The difference between the strings is that 99 of them have colon, dashes, or both and the one other string has neither. I tested the regex multiple times. The string without dashes or colons should go the else clause and use the ‘default’ name defined at the top of the for loop.
You’re doing some very odd things here. Let’s go through a few of them.
1 –
name = jobs[j]['Job_Name'][0]It seems to me that perhaps jobs is a container of dictionaries, which have a key ‘Job_Name’, and that key’s value is a list or tuple. I think what you might mean is:
name = j['Job_name'][0]If jobs is just a list of strings, being job names, all you need is this:
name = j, which you can just skip – and just usejwhen you want to refer to it.2 –
p=re.compile("(\\w*):?\\w*-?\\d*")You’re compiling your regex every time you go through the loop, where it is exactly the same each time, and you use it exactly once. Do this compilation before the loop begins.
3 – You don’t need your else clause at all. And if you want “else if” in Python, you use “elif”.
4 – Your regex could be simplified by using a raw string to eliminate all the double escapes:
p=re.compile(r"(\w*):?\w*-?\d*")5 – Your whole regex may be unnecessary. If you’re doing what I think you’re doing, you just want what appears before the first colon, if there is one. (Unless you really mean
group(1), which I think you don’t.) Which means you could skip the regex (since you’re matching withinanyhow), and just use something likename = name.split(':')[0]