Regular Expression

Pippo adm2303.2304 at gmail.com
Sun Apr 12 22:28:18 EDT 2015


> 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.

Thanks Cameron, this worked. 

> 
> 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).
 

Now I have this input data:

#D{#C[Health] #P[Information] - 
means any information, including #ST[genetic information], 
whether #C[oral | (recorded in (any form | medium))], that 
(1)#C[Is created or received by] a 
#A[health care provider | health plan | public health authority | employer | life insurer | school | university | or health care clearinghouse];  
(2)#C[Relates to] #C[the past, present, or future physical | mental health | condition of an individual] | 
#C[the provision of health care to an individual] | 
#C[the past, present, or future payment for the provision of health care to an individual].}

The result of my code is this:
#C[Health]
[<_sre.SRE_Match object at 0x10212ee40>]
[<_sre.SRE_Match object at 0x10212ee40>]
[<_sre.SRE_Match object at 0x10212ee40>]
[<_sre.SRE_Match object at 0x10212ee40>]
[<_sre.SRE_Match object at 0x10212ee40>]
[<_sre.SRE_Match object at 0x10212ee40>]
[<_sre.SRE_Match object at 0x10212ee40>]
[<_sre.SRE_Match object at 0x10212ee40>]

The code itself:
import re
import tkinter.filedialog
import readfile

j = 0
text = []

content = readfile.pattread()

constraint = re.compile(r'(#C\[\w*\])')

while j < len(content):

    result = constraint.search(content[j])
    if result is not None: 
        text.append(result)
        print(result.group(0))
    print(text)
    j = j+1

I know that "[<_sre.SRE_Match object at 0x10212ee40>]" is the result of print(text). Does this mean that we ONLY found one match object?

What about if I want the code finds also this one? #C[Is created or received by]?
or this one?
#C[the past, present, or future physical | mental health | condition of an individual]

Is it because of the space and other characters that it doesn't match them?



More information about the Python-list mailing list