tags:

views:

46

answers:

2
expr = "name + partner_id.country_id.name + city + '  ' + 123 + '123' + 12*2/58%45"

print re.findall('\w+[.]',expr)
['name',
 'partner_id',
 'country_id',
 'name',
 'city',
 '123',
 '123',
 '12',
 '2',
 '58',
 '45']

I want to include "." so result should be like

['name',
 'partner_id.country_id.name',
 'city',
 '123',
 '123',
 '12',
 '2',
 '58',
 '45']
+1  A: 

Try this:

re.findall('[\w.]+',expr)

This finds blocks of characters made of letters, numbers, underscores and dots.

Your original regex finds a word followed by a single dot, so I don't see how you got the posted results: http://codepad.org/Khsd6IuW .

Kobi
+1 for pointing the original regex did not produce the shown result.
codaddict
+2  A: 

Try the regex:

[\w.]+

Explanation:

  • [...] is the char class
  • \w is a char of a word, short for [a-zA-Z0-9_]
  • . is generally a meta char to match any char but inside a char class its treated as a literal .
  • + for one or more
codaddict