Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6744023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:02:03+00:00 2026-05-26T12:02:03+00:00

Is there any way that regular expression match inside a if block would interfere

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T12:02:04+00:00Added an answer on May 26, 2026 at 12:02 pm

    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 use j when 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 with in anyhow), and just use something like name = name.split(':')[0]

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Having the following regular expression: ([a-z])([0-9])\1 It matches a5a , is there any way
I have been trying real hard understanding regular expression, Is there any way I
Is there any way that I can change how a Literal of a code
Is there any way that I can pass arguments in selector? example: I have
Using regular expressions (in any language) is there a way to match a pattern
I have a problem with regular expressions. I would like to match strings that
Is there any way that I can programmatically create (and I guess access) hidden
Is there any way that I could get the source of a website (as
Is there any way that I can find the container pointed to by an
Is there any way that I can create a new delegate type based on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.