1

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".

Tom
  • 11
  • 1
  • What bad thing would happen if you converted some "x"s into "x"s? ;-) – Piotr Siupa Aug 21 '22 at 23:18
  • I don't see a way to combine this two replacements into one. Maybe someone else will come out with something but probably it cannot be done, or at least it cannot without tremendously overcomplicating this. – Piotr Siupa Aug 21 '22 at 23:30
  • Thanks @NO_NAME for response. Now I think, I can do [^0-3a-zA-Z] instead of [- ]. It will be more flexible, if someone input . (dot) or other special character instead of space or dash. Second pattern are fine. – Tom Aug 21 '22 at 23:48

1 Answers1

1

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'
The fourth bird
  • 154,723
  • 16
  • 55
  • 70