0

I'm looking for a regex which match all variables in an expression, which could only be letters followed by a number, like "x1" or not followed by a number, juste like "z". And I want to find them in expressions where they can be followed by all characters, for example

expr = exp(x36)+log(x27)+2*z

For example the regex should return [x36, x27, z]

I have tried this :

pattern = re.findall("[^a-z][a-z][^a-z](\d?)", expr)

where [^a-z][a-z][^a-z] means "not a letter followed by a letter itself followed by not a letter" but it does not seems to work, it returns to me a [] list

1 Answer 1

1

Use

re.findall(r'\b[A-Za-z]\d*\b', exp)

See regex proof.

EXPLANATION

--------------------------------------------------------------------------------
  \b                       the boundary between a word char (\w) and
                           something that is not a word char
--------------------------------------------------------------------------------
  [A-Za-z]                 any character of: 'A' to 'Z', 'a' to 'z'
--------------------------------------------------------------------------------
  \d*                      digits (0-9) (0 or more times (matching
                           the most amount possible))
--------------------------------------------------------------------------------
  \b                       the boundary between a word char (\w) and
                           something that is not a word char

Python code:

import re
regex = r"\b[A-Za-z]\d*\b"
test_str = "exp(x36)+log(x27)+2*z"
print(re.findall(regex, test_str))

Results: ['x36', 'x27', 'z']

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.