Regular Expression

Pippo adm2303.2304 at gmail.com
Sun Apr 12 21:28:34 EDT 2015


On Sunday, 12 April 2015 21:21:48 UTC-4, Cameron Simpson  wrote:
> On 12Apr2015 17:55, Pippo <adm2303.2304 at gmail.com> wrote:
> >On Sunday, 12 April 2015 20:46:19 UTC-4, Cameron Simpson  wrote:
> >> It looks like it should, unless you have mangled your regular expression.  
> [...]
> >> Also note that you can print the regexp's .pattern attribute:
> >>   print(constraint.pattern)
> >> as a check that what was compiled is what you intended to compile.
> [...]
> >This is the complete code:
> [...]
> 
> Thank you.
> 
> >while j < len(content):
> >    constraint = re.compile(r'(#C\[\w*\])')
> >    result = constraint.search(content[j])
> 
> I notice you seem to have incorporated MRAB's suggestion that the re.MULTILINE 
> is a parameter for re.compile, not re.search, and also my suggestion that you 
> don't need it at all.
> 
> >    text.append(result)
> >    print(constraint.pattern)
> >    print(text)
> >    print(repr(content[j]))
> >    j = j+1
> >
> >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.
> 
> Then there are assorted things that can be done to improve the code in general, 
> but could you see if you actual bugs are now fixed?
> 
> Cheers,
> Cameron Simpson <cs at zip.com.au>
> 
> A program in conformance will not tend to stay in conformance, because even if
> it doesn't change, the standard will.   - Norman Diamond <diamond at jit.dec.com>

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'




More information about the Python-list mailing list