简体   繁体   中英

Using regex to grep a string in Python

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM