473,287 Members | 1,708 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

Help needed with a regular expression

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
Nov 15 '05 #1
4 3170
Hi,
[inline]

"Neri" <av****@lycos.co.uk> wrote in message
news:87**************************@posting.google.c om...
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

Nov 15 '05 #2
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" <bm**************@hotmail.com> wrote in message news:<et********************@phobos.telenet-ops.be>...
Hi,
[inline]

"Neri" <av****@lycos.co.uk> wrote in message
news:87**************************@posting.google.c om...
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

Nov 15 '05 #3

"Neri" <av****@lycos.co.uk> wrote in message
news:87*************************@posting.google.co m...
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" <bm**************@hotmail.com> wrote in message

news:<et********************@phobos.telenet-ops.be>...
Hi,
[inline]

"Neri" <av****@lycos.co.uk> wrote in message
news:87**************************@posting.google.c om...
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

Nov 15 '05 #4
Man, you are the Regular Expressions King! There is no regular
expression that can express my appreciation. :)

"BMermuys" <bm**************@hotmail.com> wrote in message news:<QK*********************@phobos.telenet-ops.be>...
"Neri" <av****@lycos.co.uk> wrote in message
news:87*************************@posting.google.co m...
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" <bm**************@hotmail.com> wrote in message

news:<et********************@phobos.telenet-ops.be>...
Hi,
[inline]

"Neri" <av****@lycos.co.uk> wrote in message
news:87**************************@posting.google.c om...
> 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

Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Steve | last post by:
Hello, I am writing a script that calls a URL and reads the resulting HTML into a function that strips out everthing and returns ONLY the links, this is so that I can build a link index of various...
2
by: Jack Smith | last post by:
Can someone help me out with this problem. Any help is appreciated. Thanks. Let doubleswap(x) be the string formed by replacing each a in x by the substring bb and each b by the substring aa....
2
by: Jack Smith | last post by:
I posted this question earlier, but I got no responses. Can anyone help me out here...any hints or even how to start? Thanks in advance. Let doubleswap(x) be the string formed by replacing each...
4
by: pekka niiranen | last post by:
Hi there, I have perl script that uses dynamically constructed regular in this way: ------perl code starts ---- $result ""; $key = AAA\?01; $key = quotemeta $key; $line = " ...
5
by: tmeister | last post by:
I am in need of a regular expression that tests and fails if there are 14 or more of a character in the test string. There can be up to 13 of these characters in the string and any other...
4
by: henrik | last post by:
Hi I have a regex question. I want to find all content of a <td class="someclass"> tag. This means the expression should include all other tags included between <td class="someclass"> and </td>....
1
by: Rahul | last post by:
Hi Everybody I have some problem in my script. please help me. This is script file. I have one *.inq file. I want run this script in XML files. But this script errors shows . If u want i am...
3
by: Willing 2 Learn | last post by:
Hey, I'm trying to teach myself C++ and I came across 3 problems. I understand the concept of FSA but getting the C++ code to do it as become an issue. Only thing is im clueless as to how to do...
1
by: BHPexpert | last post by:
Regular Expression help needed -------------------------------------------------------------------------------- I want to extract all text that is contained inside the brackets after the word...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.