Regex Doubts

Antoon Pardon antoon.pardon at vub.be
Fri Mar 30 05:21:09 EDT 2018


On 30-03-18 08:16, Iranna Mathapati wrote:
> Hi Team,
>
>
> how to achieve fallowing expected output?
>
> str_output= """
>
> MOD1 memory                              : 2 valid    1790 free
> MOD2 memory                              : 128 valid   128 free
> UDP Aware *MEMR*                            : 0 valid   0 free *MEMR
>                                    : 21 valid   491 free
> <<<<<expecting *
> Feature XYZ                              : 3 valid    13 free
> Feature PQR                       : 0 valid 16 free
> Feature MNO                        : 0 valid 2 free
>
>  """
>
> i am going to grep MEM values alone and i have tried fallowing Regex:
>
> re.findall(r'MEMR\s+\:\s+([0-9]+)\s+valid \s+([0-9]+)\s+free ', str_output)
>
> it produce fallowing output::
> [('0', '0'), ('21', '491')]
>
> Expected output and i am expecting fallowing output::
> *[('21' ,'491')]  <<<<< how to achieve this output?*

I find it hard to find a good answer to your question because you give no
reason why you want to reject the ('0', '0') result.

Are you uninterested in 0 results?
Are you only interested in lines that start with MEMR and so are not interested
in lines where the MEMR comes later?

In the first case you can write:
print(re.findall(r'MEMR\s+\:\s+([1-9][0-9]*)\s+valid \s+([1-9][0-9]*)\s+free', str_output))

In the second case you can write:
regex = re.compile(r'^MEMR\s+\:\s+([0-9]+)\s+valid \s+([0-9]+)\s+free', re.MULTILINE)
print(regex.findall(str_output))

 




More information about the Python-list mailing list