Python Regex pattern not being matched -


i'm trying regex parse values stored in set of lua files, each line looks 1 of these 2 lines

  1. item.id = 'item_clock';\r\n
  2. item.cost = 150;\r\n.

when run regex pattern on first line expected result

>>> re.search("item.(?p<key>[a-za-z]\w) = (?p<value>.*);", line).groupdict() {'key': 'id', 'value': "'item_clock'"} 

however when run on second line, don't match object.

the regex looks item. followed letter followed exactly one word character (the \w in regex).

you meant item.(?p<key>[a-za-z]\w*)... (note added asterisk). item. followed letter followed zero or more word characters.

also, it's idea use raw strings regular expressions avoid hard-to-spot bugs:

r"item.(?p<key>[a-za-z]\w*) = (?p<value>.*);" 

(note r prefix).


Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -