Regular Expression

Cameron Simpson cs at zip.com.au
Mon Apr 13 02:46:00 CEST 2015


On 12Apr2015 17:25, Pippo <adm2303.2304 at gmail.com> wrote:
>> >> > >      constraint = re.compile(r'(#C\[\w*\]'))
>> >> > >      result = constraint.search(content[j],re.MULTILINE)
>> >> > >      text.append(result)
>> >> > >      print(text)
[...]
>> >> result is empty! Although it should have a content.
[...]
>> > I fixed the syntax error but the result shows:
>> > [None]
>> > [None, None]
[...]

Note that "None" is not "empty", though in this case more or less means what 
you think.

You're getting None because the regexp fails to match.

>> Try printing each string you're trying to match using 'repr', i.e.:
>>      print(repr(content[j]))
>>
>> Do any look like they should match?
> print(repr(content[j])) gives me the following:
>
>[None]
>'#D{#C[Health] #P[Information] - \n'
[...]
>shouldn't it match "#C[Health]" in the first row?

It looks like it should, unless you have mangled your regular expression. You 
mentioned earlier that you fixed the syntax error, but you never actually 
recited the line after fixing it. Please cut/paste the _exact_ line where you 
compile the regexp as it is now.  Superficially I would expect your regexp to 
work, but we would like to see it as it current is.

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.

>If not, what is the best way to fetch these items in an array?

What you've got is ok. I would point out that as you're processing each line on 
its own you should not need "re.MULTILINE" in your .compile() call.

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

The upside of PHP is that it lets non-programmers create complex
applications. The downside of PHP is that it lets non-programmers create
complex applications. - Elliot Lee



More information about the Python-list mailing list