python如何从字符串中提取运算符

时间:2022-03-08 19:17:06

I want to extract operators like: +,-,/,* and also (,),_ from the string

我想从字符串中提取运算符,如:+, - ,/,*以及(,),_

Eg.

a-2=b
(c-d)=3

output:

- ,=, (, -, ), = 

This does not work:

这不起作用:

re.finditer(r'[=+/-()]*', text)

1 个解决方案

#1


6  

Your re needs to escape some of the characters with a backslash. (+, -, (, ) have their special meanings in re).

你需要用反斜杠来逃避一些角色。 (+, - ,(,)在re中有其特殊含义)。

Anyway, for this you don't need re:

无论如何,为此您不需要重新:

(c for c in s if c in '+-/*()_')

#1


6  

Your re needs to escape some of the characters with a backslash. (+, -, (, ) have their special meanings in re).

你需要用反斜杠来逃避一些角色。 (+, - ,(,)在re中有其特殊含义)。

Anyway, for this you don't need re:

无论如何,为此您不需要重新:

(c for c in s if c in '+-/*()_')