I suck at Python regex and would love to see some solved examples to help me gain understanding. I am experimenting using http://pyregex.com/ which is great but need some 'good' examples to get me started.
I try to create a set of rules like so:
rules = [('name', r'[a-z]+'),
('operator', r'[+-*\]']
which I have found but not confident enough to create my own regexes for cases like the ones listed below:
- match only the
=or+=or*=characters - match the
+character (i.e theoperatoras seen above) separately from the++characters - match any one word after a specific keyword (e.g.
int) and any number of space(s) and/or tabs. [edited - initially had followed which was wrong]
For 1. I have tried [\+=|=], for 2. I know the order in the rules is important and for 3. I am completely lost with the [] and on how I can generalize that case to work not just for int, but for float as well.
Any code examples will be greatly appreciated since I am only just starting with Python and coding!
r'[+*]?=', 2)r'(?<!\+)\+(?!\+)', 2)r'\b\w+\b(?=\s+int\s+)'.r[=*+]+for the first one also matches==,++,**etc. whereas I want just to match the=or+=or*=?for the first one, could we not explicitly provide++and**? If so, how can put characters in a set when they are not single-chars (like++)?