Reputation: 11
I need to create a short pattern for my script.Can someone tell me, my code is ok, or could better?
import re
cipher = input("Cipher: ")
pattern1 = re.sub("[- ]", "", cipher)
pattern2 = re.sub("[^0-3x]", "x", pattern1)
Input must be specific, that mean can contains only digits and lowercase letter/s "x". Because sometimes a person will could input something with space or dash in my case I must check it. So first pattern remove it, if existed, and second pattern replace any characters that are not my digits or "x" to "x". In that case, the second pattern is in some way a safeguard against destroying script. My output must contain only digits from 0 to 3 and one or more char "x". Again, "x" is something specific, but person can input other letter/signs, and I want not accept, that, so it will be converted to "x".
About pattern1, I know, I can do the same with string methods: .replace(" ", "").replace("-", "")
but regular expressions look nice.
If someone input: 11a-223 ??b, I need output: 11x223xxx and so on with another examples.
This not work: pattern1 = re.sub("- ", "", cipher) and re.sub("[^0-3x]", "x", cipher)
Question: both patterns can be combined in one? Pattern should remove first any space and/or dash, and next convert everything that is not 0-3 and char "x" to char "x".
Upvotes: 1
Views: 58
Reputation: 163457
If you want to use 1 pattern with 2 different replacements, you can use an alternation |
capture group for one of the parts like ([- ])
([- ])|[^0-3x]
Then use a lambda with re.sub and check for the group 1 value.
Based on that result, you can do the replacements.
import re
strings = [
"11a-223 ??b",
"---",
" ",
" - -",
"1234567890- 1234567890!@#$%^&*()_+-"
]
pattern = r"([- ])|[^0-3x]"
for s in strings:
print("'{}'".format(re.sub(pattern, lambda x: "" if x.group(1) else "x", s)))
Output
'11x223xxx'
''
''
''
'123xxxxxx0123xxxxxx0xxxxxxxxxxxx'
Upvotes: 1