regex help

J

jeff sacksteder

Regex questions seem to be rather resistant to googling.

My regex currently looks like - 'FOO:.*\n\n'

The chunk of text I am attempting to locate is a line beginning with
"FOO:", followed by an unknown number of lines, terminating with a
blank line. Clearly the ".*" phrase does not match the single newlines
occuring inside the block.

Suggestions are warmly welcomed.
 
C

Christopher Subich

jeff said:
Regex questions seem to be rather resistant to googling.

My regex currently looks like - 'FOO:.*\n\n'

The chunk of text I am attempting to locate is a line beginning with
"FOO:", followed by an unknown number of lines, terminating with a
blank line. Clearly the ".*" phrase does not match the single newlines
occuring inside the block.

Include the re.DOTALL flag when you compile the regular expression.
 
J

John Machin

jeff said:
Regex questions seem to be rather resistant to googling.

My regex currently looks like - 'FOO:.*\n\n'

The chunk of text I am attempting to locate is a line beginning with
"FOO:", followed by an unknown number of lines, terminating with a
blank line. Clearly the ".*" phrase does not match the single newlines
occuring inside the block.

Suggestions are warmly welcomed.

I suggest you read the manual first:
"""
"."
(Dot.) In the default mode, this matches any character except a newline.
If the DOTALL flag has been specified, this matches any character
including a newline.
"""
 
?

????? ????? =?ISO-8859-1?Q?=28Shantanoo_Mahajan=29

John said:
I suggest you read the manual first:
"""
"."
(Dot.) In the default mode, this matches any character except a newline.
If the DOTALL flag has been specified, this matches any character
including a newline.
"""

I think you need to write you own function. Something like:

for x in open('_file_name'):
if x == 'Foo:\n':
flag=1
if x == '\n':
flag=0
if flag == 1:
print x


if the line is 'FOO: _some_more_data_' you may try,
if x.startswith('Foo:'):
instead of
if x == 'Foo:\n':

Hope this help.

Shantanoo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,262
Messages
2,571,311
Members
47,986
Latest member
ColbyG935

Latest Threads

Top