0

I have tried

if re.match('.*[a-zA-Z].*' ,funcion) and not re.match("[x]", funcion):

It actually works, but I want to make my code smaller and I guess there is an option for reducing it. Like re.match('.*[a-zA-Z]^x.*' ,funcion), but it doesnt really work.

UPDATE:

I just made this:

if re.match('.*[a-zA-Z].*' ,funcion) and not re.match("x", funcion) and not re.match("[1-9]", funcion):

This works well, because I want to put functions like 7x + 3, and doing the upper thing wont work with 7x because it detects it. There should be a way to make this a pattern..

FULL CODE OF MY PROGRAM

while True:
  
  
   funcion = input("")
   if re.match('.*[a-zA-Z].*' ,funcion) and not re.match("x", funcion) and not re.match("[1-9]", funcion):
    print("Incorrecto, contiene una letra")
    
  

   elif "x" not in funcion:
      print("No contiene la X necesaria en una funcion.")

   else:
    break
"

2 Answers2

2

You may express the pattern as ^[a-wyz]+$:

inp = ["Hello", "World", "taxes"]
for i in inp:
    if re.search(r'^[a-wyz]+$', i, flags=re.I):
        print("MATCH:    " + i)
    else:
        print("NO MATCH: " + i)

This prints:

MATCH:    Hello
MATCH:    World
NO MATCH: taxes
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
0

maybe this will work:

re.match('^[^xX]+$', text)
Jupri
  • 345
  • 1
  • 6