shahjapan
shahjapan

Reputation: 14335

regular expression help which includes "." in word separation

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

Upvotes: 0

Views: 71

Answers (2)

codaddict
codaddict

Reputation: 455132

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

Upvotes: 2

Kobi
Kobi

Reputation: 138027

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 .

Upvotes: 1

Related Questions