2

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>']
2
  • what is your expected Output? Commented Jun 5, 2015 at 6:18
  • All of k (first line): ['<xyz>27*</xyz>', 'name="namePad">22</xyz>'] Commented Jun 5, 2015 at 6:20

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

What is the ? after * for?
It will still match the numbers, in case * doesn't exist.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.