1

I want to run for loop to generate new regex for checking in the file for example

import re
K = "MS-85409/LN-85409/L-1"
le = ["L-1","L-11","L-112"]
for i in le:
    s="[A-Z]+-+[0-9]+/"+i+"$"
    x = re.search(r's,K)
    if x:
        do something
    else:
        pass

where i want to do is,

on first loop of for s be "[A-Z]+-+[0-9]+/"+L-1$" and put that in x=re.search(r'[A-Z]+-+[0-9]+/"+L-1$',K) and search it in K if matches do something and then second loop s will be "[A-Z]+-+[0-9]+/"+L-11$" and put that in x=re.search(r'[A-Z]+-+[0-9]+/"+L-11$',K) and search it in K if matches do something and so on..

1
  • the problem is I can't pass s into the string which is 's' in this case and If I am passing it like re.search(r " ' "+s,K) it says r is not defined. and If I am passing it like re.search(r'[A-Z]+-+[0-9]+/"+L-1$',K) it works perfectly. Commented Aug 13, 2020 at 10:12

1 Answer 1

1

Change those 2 lines:

s="[A-Z]+-+[0-9]+/"+i+"$"
x = re.search(r's,K)

into:

s=r"[A-Z]+-+[0-9]+/"+i+"$"
x = re.search(s,K)
Sign up to request clarification or add additional context in comments.

Comments

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.