Regex Doubts

Iranna Mathapati iranna.gani28 at gmail.com
Fri Mar 30 03:36:36 EDT 2018


Hi Cameron.


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
Feature XYZ                            : 3 valid    13 free
Feature PQR                           : 0 valid    16 free
Feature MNO                          : 0 valid    2 free
"""

pattern = 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')]


[('21' ,'491')]  <<<<< how to achieve this output using *"findall with
single line code"*?

NOTE "MEMR" values are 0 or more then 0 valid/free values

Thanks,
Iranna

On Fri, Mar 30, 2018 at 12:43 PM, Cameron Simpson <cs at cskk.id.au> wrote:

> On 30Mar2018 11:46, Iranna Mathapati <iranna.gani28 at gmail.com> wrote:
>
>> 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 get an empty list with your str_output text. Is it intact above? Or
> modified by the mail program?
>
> That said, I see that your regexp ends with "free ". Are you sure there is
> a space after the word "free" in your input data? There doesn't seems to be
> one in your email. Because your regexp requires a final space, if there
> isn't one in the input then it will not match.
>
> It may be that your original data has this final space, hence your matches.
>
> In which case, there are two "MEMR" words in your input data. Are there
> really asterisks ("*") in the input data? Or are they for emphasis? I ask
> because you've got some lines ending in "::" in your email, which is
> suggestive of some formatting.
>
> So, supposing that the asterisks are not there, you have two "MEMR : N
> valid   M free" strings up there. It sounds like you want only the nonzero
> one. Notice that your regexp includes:
>
>  [0-9]+
>
> to match 1 or more digits. If you don't want to recognise zero values,
> consider that any such number doesn't commence with a "0" digit. How would
> you modify the regexp above to be more picky?
>
> Cheers,
> Cameron Simpson <cs at cskk.id.au>
>



More information about the Python-list mailing list