Regular Expression

Cameron Simpson cs at zip.com.au
Mon Apr 13 04:09:53 CEST 2015


On 12Apr2015 18:28, Pippo <adm2303.2304 at gmail.com> wrote:
>On Sunday, 12 April 2015 21:21:48 UTC-4, Cameron Simpson  wrote:
[...]

Pippo, please take a moment to trim the less relevant verbiage from the quoted 
material; it makes replies easier to read because what is left can be taken to 
be more "on point". Thanks.

Discussion below.

>> >This is the result I get:
>> >(#C\[\w*\])
>> >[<_sre.SRE_Match object at 0x10292ee40>]
>> >'#D{#C[Health] #P[Information] - \n'
>> >(#C\[\w*\])
>> >[<_sre.SRE_Match object at 0x10292ee40>, None]
>>
>> I think you've missed the first line, but the above output looks correct to me
>> at this point.
>>
>> You may want to modify your code to say something like this:
>>     if result is not None:
>>         text.append(result)
>> so that you only accrue matches in your text list.
>
>Thanks Cameron, I added the if statement but I still get this error:
>
>Traceback (most recent call last):
>  File "/Users/sepidehghanavati/Desktop/Programs/python/Python3.3/Main Modules/testpattern.py", line 24, in <module>
>    print(result.group(0))
>AttributeError: 'NoneType' object has no attribute 'group'

Certainly, that is to be expected; you're printing that unconditionally, even 
when "result" is None.

Put the print inside the "if"; you don't really care when result is None, and 
anyway you can't access .group when it is None - it is not an 're.match" 
object, because there was no match.

Once you're happy you should consider what happens when there is more than one 
"C#[blah]" in a line (if that happens; you know the data better than we).

Cheers,
Cameron Simpson <cs at zip.com.au>

This is my simple religion. There is no need for temples; no need for
complicated philosophy. Our own brain, our own heart is our temple; the
philosophy is kindness.	- Dalai Lama



More information about the Python-list mailing list