Python how to extract statements from a string

I want to extract operators like: +,-,/,* , as well as (,),_ from a string

Eg.

 a-2=b (cd)=3 

output:

 - ,=, (, -, ), = 

This does not work:

 re.finditer(r'[=+/-()]*', text) 
+4
source share
1 answer

In your re you need to avoid some backslash characters. ( + , - , ( , ) have their special meanings in re ).

In any case, you do not need re for this:

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

All Articles