I'm currently learning about pythons regular expressions but can't find a way to solve the following issues.
I want to insert a variable into my regex. I can do that comfortably with the "old" style using %.
However if I use format() and my regex contains a {} for the number of allowed occurrences, it gets problematic.
For example: a = re.compile("[{0}]{1}[{1}]{1}".format(exp1, exp2))
Is there some kind of work around?
Also i was wondering how you can have a negative range.
For example: a = re.compile("[A]{1}[(-1)-4]{1}")
Other post suggest something like [-+]?
, but that matches too much.
I only want A-1, A0, A1, A2, A3 and A4 to be allowed.
I'd appreciate any kind of help.
Asked
Active
Viewed 28 times
0

PiddBoo
- 31
- 3
-
ItIt is always complicated to have two questions together - in this case the first one was a duplicate. The answer to your second question is: Use an "OR" in regex: `A(-1|[0-4])` should do the trick. General hint: Check the online regex editor regex101, it allows you to write your own regex and try it on any data you like. – ingofreyer Nov 29 '17 at 10:22
-
I really appreciate your reply. I was looking carefully through the other posts but seem to have missed the thread to the first question. Thank you very much for your help though. – PiddBoo Nov 29 '17 at 13:36
-
You are welcome. Feel free to ask further questions but maybe stick to one question at a time ;-) – ingofreyer Nov 29 '17 at 16:57