Reputation: 3081
I am trying to grep two kinds of patterns in a list using re in python:
'<xyz>number followed by optional *</xyz>'
'name="namepad">number</xyz>
Using regex in python, I am not able to get the data with asterisk. Here is a sample session, what can I do so that the filter also returns the first element?
>>> k = ['<xyz>27*</xyz>', 'name="namePad">22</xyz>']
>>> f = filter(lambda x:re.search('^name="namePad"|^<xyz>[0-9]{1,3}\*" <\/xyz>',x), k)
>>> f
['name="namePad">22</xyz>']
Upvotes: 2
Views: 1077
Reputation: 61235
You can use re.match
since to check for a match only at the beginning of the string. Also you don't need filter use list comprehensions instead.
>>> [i for i in k if re.match(r'(<xyz>|name="namePad">)\d+\*?', i)]
['<xyz>27*</xyz>', 'name="namePad">22</xyz>']
The ?
after *
mean that *
is optional you can read more about quantifiers Here
Upvotes: 1
Reputation:
Your regex has mismatched "
quotes. Try this:
filter(lambda x:re.search(r'^name="namePad"|^<xyz>[\d]{1,3}\*?</xyz>',x), k)
It will give you the following:
['27*', 'name="namePad">22']
Upvotes: 1