Help needed with a regular expression

N

Neri

Some document processing program I write has to deal with documents
that have headers and footers that are unnecessary for the main
processing part. Therefore, I'm using a regular expression to go over
each document, find out if it contains a header and/or a footer and
extract only the main content part.

The headers and the footers have no specific format and I have to
detect and remove them using a list of strings that may appear as
seperators between the header (or the footer) and the content. These
strings are allowed to act as seperators only if they appear in a
seperate line, that may contain only whitespaces, commas, dots, etc.
but not letters or digits. In addition, the headers and the footers
don't appear in all the documents, so I have to consider this
possibility too.

For example, the list of headers may contain the following strings:
"header ended", "here comes the content", "hhhhh". The list of footers
may contain the strings: "footer begins", "fffff". The following
document sample has a header but no footer:

To: George W. Bush
Subject: Hello!
hhhhh ,/
bla bla bla
bla bla fffff bla bla this is not a footer since
the seperator string doesn't come in a seperated line

Well, here's a regular expression I managed to write:

(?<header>.*^\W*(header ended|hhhhh|here comes the
content)\W*$|)(?<content>.+)(?<footer>^\W*(footer
begins|fffff)\W*$.*|)

When I run it with the options Multiline and Singleline turned on, it
works fine for the header, but the content capture group of the
regular expression is "greedy" and it captures the footer too. Notice
that the reason it can do so is that the footer may be a zero-length
string in order to deal with documents that have no footer.

Now, I tried to fix it by making the content capture group "lazy":
(?<content>.+?)
Well, this helped capture the footer, but now the content group
captures every content character in a seperate match. If I want to use
it, I have to append all the matches using a StringBuilder...

Any ideas?

BTW, I know I can do it using two regular expressions and two phases,
but I'm looking for a single regular expression that will capture the
content without further processing.

Thanks,

Avner
 
B

BMermuys

Hi,
[inline]

Neri said:
Some document processing program I write has to deal with documents
that have headers and footers that are unnecessary for the main
processing part. Therefore, I'm using a regular expression to go over
each document, find out if it contains a header and/or a footer and
extract only the main content part.

The headers and the footers have no specific format and I have to
detect and remove them using a list of strings that may appear as
seperators between the header (or the footer) and the content. These
strings are allowed to act as seperators only if they appear in a
seperate line, that may contain only whitespaces, commas, dots, etc.
but not letters or digits. In addition, the headers and the footers
don't appear in all the documents, so I have to consider this
possibility too.

For example, the list of headers may contain the following strings:
"header ended", "here comes the content", "hhhhh". The list of footers
may contain the strings: "footer begins", "fffff". The following
document sample has a header but no footer:

To: George W. Bush
Subject: Hello!
hhhhh ,/
bla bla bla
bla bla fffff bla bla this is not a footer since
the seperator string doesn't come in a seperated line

Well, here's a regular expression I managed to write:

(?<header>.*^\W*(header ended|hhhhh|here comes the
content)\W*$|)(?<content>.+)(?<footer>^\W*(footer
begins|fffff)\W*$.*|)

Try this:

string rex = @"\A((?<head>.*?)^\W*(hhhhh|header ended|here comes the
content)\W*$)?(?<content>.*?)(^\W*(fffff|footer begins)\W*$(?<foot>.*))?\Z";

You must use the Single _and_ Multiline option. Optional you can use the
explicit capture option too.

The named capture groups head, content and foot do not contain the
delimiter(s).

\A and \Z indicate the begin and end of the text, it forces a complete
parsing of the text.

^ $ indicate the begin and end of a line (when multiline is set).

\W means non-alphanumeric characters, however _ (underscore) is considered
alphanumeric, if you which to have the underscore not considered
alphanumeric you can replace \W by [^a-zA-Z0-9].

The entire header (text + delimiter) is contained inside (...)?, which means
zero or one time, the same is done for the footer.

HTH,
Greetings
 
N

Neri

Thanks, it works! However, I forgot to mention one tiny requirement
that might make this much harder: the regular expression must perform
lazy lookup of both the header seperator and the footer seperator.
This means that if there are two (or more) lines in the document that
match the pattern of the header or the footer, only the first line
should be matched as a header seperator and only the last line should
be matched as a footer seperator.

Here's a document sample as an example:

This is the header.
hhhhh
This is no longer the header, it's a part of the content.
hhhhh
This is also a part of the content.
fffff
This isn't a part of the footer. Not yet.
fffff
This is the footer.

The regular expression suggested in BMermuys's reply will match the
first 2 lines as the header perfectly, however, the last 4 lines will
match as the footer instead of only the last 2 lines.

Any ideas?

Thanks again,

Avner

BMermuys said:
Hi,
[inline]

Neri said:
Some document processing program I write has to deal with documents
that have headers and footers that are unnecessary for the main
processing part. Therefore, I'm using a regular expression to go over
each document, find out if it contains a header and/or a footer and
extract only the main content part.

The headers and the footers have no specific format and I have to
detect and remove them using a list of strings that may appear as
seperators between the header (or the footer) and the content. These
strings are allowed to act as seperators only if they appear in a
seperate line, that may contain only whitespaces, commas, dots, etc.
but not letters or digits. In addition, the headers and the footers
don't appear in all the documents, so I have to consider this
possibility too.

For example, the list of headers may contain the following strings:
"header ended", "here comes the content", "hhhhh". The list of footers
may contain the strings: "footer begins", "fffff". The following
document sample has a header but no footer:

To: George W. Bush
Subject: Hello!
hhhhh ,/
bla bla bla
bla bla fffff bla bla this is not a footer since
the seperator string doesn't come in a seperated line

Well, here's a regular expression I managed to write:

(?<header>.*^\W*(header ended|hhhhh|here comes the
content)\W*$|)(?<content>.+)(?<footer>^\W*(footer
begins|fffff)\W*$.*|)

Try this:

string rex = @"\A((?<head>.*?)^\W*(hhhhh|header ended|here comes the
content)\W*$)?(?<content>.*?)(^\W*(fffff|footer begins)\W*$(?<foot>.*))?\Z";

You must use the Single _and_ Multiline option. Optional you can use the
explicit capture option too.

The named capture groups head, content and foot do not contain the
delimiter(s).

\A and \Z indicate the begin and end of the text, it forces a complete
parsing of the text.

^ $ indicate the begin and end of a line (when multiline is set).

\W means non-alphanumeric characters, however _ (underscore) is considered
alphanumeric, if you which to have the underscore not considered
alphanumeric you can replace \W by [^a-zA-Z0-9].

The entire header (text + delimiter) is contained inside (...)?, which means
zero or one time, the same is done for the footer.

HTH,
Greetings
When I run it with the options Multiline and Singleline turned on, it
works fine for the header, but the content capture group of the
regular expression is "greedy" and it captures the footer too. Notice
that the reason it can do so is that the footer may be a zero-length
string in order to deal with documents that have no footer.

Now, I tried to fix it by making the content capture group "lazy":
(?<content>.+?)
Well, this helped capture the footer, but now the content group
captures every content character in a seperate match. If I want to use
it, I have to append all the matches using a StringBuilder...

Any ideas?

BTW, I know I can do it using two regular expressions and two phases,
but I'm looking for a single regular expression that will capture the
content without further processing.

Thanks,

Avner
 
B

BMermuys

Neri said:
Thanks, it works! However, I forgot to mention one tiny requirement
that might make this much harder: the regular expression must perform
lazy lookup of both the header seperator and the footer seperator.
This means that if there are two (or more) lines in the document that
match the pattern of the header or the footer, only the first line
should be matched as a header seperator and only the last line should
be matched as a footer seperator.

After re-checking, I had some problems with the original regex. Mainly with
\r\n matching. It seem that $ or \Z matches before the \n and doesn't grab
it, so if there is an \r\n, it matches in between, dividing the \r\n, which
is no good. This can be fixed by using \r\n explicit.

Furthermore, when there is no \r\n at the end of the text, then content (or
footer if there is one) will not end on \r\n. If you want all parts to end
with \r\n, make sure the text ends with \r\n.

The make sure the last footer is used, a negative (?!...) lookahead
expression is used.

string rex = @"\A((?<head>.*?\r\n)\W*(hhhhh|header ended|here comes the
content)\W*\r\n)?(?<content>.*?(\r\n)?)(\W*(fffff|footer
begins)\W*\r\n(?!.*?\r\n\W*(fffff|footer
begins)\W*\r\n)(?<foot>.*?(\r\n)?))?\Z";

(if your line-breaks are \n instead of \r\n you can safely replace all
occurences of \r\n by \n in the regex)

HTH,
greetings

Here's a document sample as an example:

This is the header.
hhhhh
This is no longer the header, it's a part of the content.
hhhhh
This is also a part of the content.
fffff
This isn't a part of the footer. Not yet.
fffff
This is the footer.

The regular expression suggested in BMermuys's reply will match the
first 2 lines as the header perfectly, however, the last 4 lines will
match as the footer instead of only the last 2 lines.

Any ideas?

Thanks again,

Avner

"BMermuys" <[email protected]> wrote in message
Hi,
[inline]

Neri said:
Some document processing program I write has to deal with documents
that have headers and footers that are unnecessary for the main
processing part. Therefore, I'm using a regular expression to go over
each document, find out if it contains a header and/or a footer and
extract only the main content part.

The headers and the footers have no specific format and I have to
detect and remove them using a list of strings that may appear as
seperators between the header (or the footer) and the content. These
strings are allowed to act as seperators only if they appear in a
seperate line, that may contain only whitespaces, commas, dots, etc.
but not letters or digits. In addition, the headers and the footers
don't appear in all the documents, so I have to consider this
possibility too.

For example, the list of headers may contain the following strings:
"header ended", "here comes the content", "hhhhh". The list of footers
may contain the strings: "footer begins", "fffff". The following
document sample has a header but no footer:

To: George W. Bush
Subject: Hello!
hhhhh ,/
bla bla bla
bla bla fffff bla bla this is not a footer since
the seperator string doesn't come in a seperated line

Well, here's a regular expression I managed to write:

(?<header>.*^\W*(header ended|hhhhh|here comes the
content)\W*$|)(?<content>.+)(?<footer>^\W*(footer
begins|fffff)\W*$.*|)

Try this:

string rex = @"\A((?<head>.*?)^\W*(hhhhh|header ended|here comes the
content)\W*$)?(?<content>.*?)(^\W*(fffff|footer
begins)\W*$(? said:
You must use the Single _and_ Multiline option. Optional you can use the
explicit capture option too.

The named capture groups head, content and foot do not contain the
delimiter(s).

\A and \Z indicate the begin and end of the text, it forces a complete
parsing of the text.

^ $ indicate the begin and end of a line (when multiline is set).

\W means non-alphanumeric characters, however _ (underscore) is considered
alphanumeric, if you which to have the underscore not considered
alphanumeric you can replace \W by [^a-zA-Z0-9].

The entire header (text + delimiter) is contained inside (...)?, which means
zero or one time, the same is done for the footer.

HTH,
Greetings
When I run it with the options Multiline and Singleline turned on, it
works fine for the header, but the content capture group of the
regular expression is "greedy" and it captures the footer too. Notice
that the reason it can do so is that the footer may be a zero-length
string in order to deal with documents that have no footer.

Now, I tried to fix it by making the content capture group "lazy":
(?<content>.+?)
Well, this helped capture the footer, but now the content group
captures every content character in a seperate match. If I want to use
it, I have to append all the matches using a StringBuilder...

Any ideas?

BTW, I know I can do it using two regular expressions and two phases,
but I'm looking for a single regular expression that will capture the
content without further processing.

Thanks,

Avner
 
N

Neri

Man, you are the Regular Expressions King! There is no regular
expression that can express my appreciation. :)

BMermuys said:
Neri said:
Thanks, it works! However, I forgot to mention one tiny requirement
that might make this much harder: the regular expression must perform
lazy lookup of both the header seperator and the footer seperator.
This means that if there are two (or more) lines in the document that
match the pattern of the header or the footer, only the first line
should be matched as a header seperator and only the last line should
be matched as a footer seperator.

After re-checking, I had some problems with the original regex. Mainly with
\r\n matching. It seem that $ or \Z matches before the \n and doesn't grab
it, so if there is an \r\n, it matches in between, dividing the \r\n, which
is no good. This can be fixed by using \r\n explicit.

Furthermore, when there is no \r\n at the end of the text, then content (or
footer if there is one) will not end on \r\n. If you want all parts to end
with \r\n, make sure the text ends with \r\n.

The make sure the last footer is used, a negative (?!...) lookahead
expression is used.

string rex = @"\A((?<head>.*?\r\n)\W*(hhhhh|header ended|here comes the
content)\W*\r\n)?(?<content>.*?(\r\n)?)(\W*(fffff|footer
begins)\W*\r\n(?!.*?\r\n\W*(fffff|footer
begins)\W*\r\n)(?<foot>.*?(\r\n)?))?\Z";

(if your line-breaks are \n instead of \r\n you can safely replace all
occurences of \r\n by \n in the regex)

HTH,
greetings

Here's a document sample as an example:

This is the header.
hhhhh
This is no longer the header, it's a part of the content.
hhhhh
This is also a part of the content.
fffff
This isn't a part of the footer. Not yet.
fffff
This is the footer.

The regular expression suggested in BMermuys's reply will match the
first 2 lines as the header perfectly, however, the last 4 lines will
match as the footer instead of only the last 2 lines.

Any ideas?

Thanks again,

Avner

"BMermuys" <[email protected]> wrote in message
Hi,
[inline]

Some document processing program I write has to deal with documents
that have headers and footers that are unnecessary for the main
processing part. Therefore, I'm using a regular expression to go over
each document, find out if it contains a header and/or a footer and
extract only the main content part.

The headers and the footers have no specific format and I have to
detect and remove them using a list of strings that may appear as
seperators between the header (or the footer) and the content. These
strings are allowed to act as seperators only if they appear in a
seperate line, that may contain only whitespaces, commas, dots, etc.
but not letters or digits. In addition, the headers and the footers
don't appear in all the documents, so I have to consider this
possibility too.

For example, the list of headers may contain the following strings:
"header ended", "here comes the content", "hhhhh". The list of footers
may contain the strings: "footer begins", "fffff". The following
document sample has a header but no footer:

To: George W. Bush
Subject: Hello!
hhhhh ,/
bla bla bla
bla bla fffff bla bla this is not a footer since
the seperator string doesn't come in a seperated line

Well, here's a regular expression I managed to write:

(?<header>.*^\W*(header ended|hhhhh|here comes the
content)\W*$|)(?<content>.+)(?<footer>^\W*(footer
begins|fffff)\W*$.*|)

Try this:

string rex = @"\A((?<head>.*?)^\W*(hhhhh|header ended|here comes the
content)\W*$)?(?<content>.*?)(^\W*(fffff|footer
begins)\W*$(? said:
You must use the Single _and_ Multiline option. Optional you can use the
explicit capture option too.

The named capture groups head, content and foot do not contain the
delimiter(s).

\A and \Z indicate the begin and end of the text, it forces a complete
parsing of the text.

^ $ indicate the begin and end of a line (when multiline is set).

\W means non-alphanumeric characters, however _ (underscore) is considered
alphanumeric, if you which to have the underscore not considered
alphanumeric you can replace \W by [^a-zA-Z0-9].

The entire header (text + delimiter) is contained inside (...)?, which means
zero or one time, the same is done for the footer.

HTH,
Greetings


When I run it with the options Multiline and Singleline turned on, it
works fine for the header, but the content capture group of the
regular expression is "greedy" and it captures the footer too. Notice
that the reason it can do so is that the footer may be a zero-length
string in order to deal with documents that have no footer.

Now, I tried to fix it by making the content capture group "lazy":
(?<content>.+?)
Well, this helped capture the footer, but now the content group
captures every content character in a seperate match. If I want to use
it, I have to append all the matches using a StringBuilder...

Any ideas?

BTW, I know I can do it using two regular expressions and two phases,
but I'm looking for a single regular expression that will capture the
content without further processing.

Thanks,

Avner
 

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

Top