BJB ChessEmperor
BJB ChessEmperor

Reputation: 25

Regex expression to find a variable in an expression

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

Upvotes: 0

Views: 104

Answers (1)

Ryszard Czech
Ryszard Czech

Reputation: 18611

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']

Upvotes: 1

Related Questions