473,699 Members | 2,835 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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|her e comes the
content)\W*$|)( ?<content>.+)(? <footer>^\W*(fo oter
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 3222
Hi,
[inline]

"Neri" <av****@lycos.c o.uk> wrote in message
news:87******** *************** ***@posting.goo gle.com...
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|her e comes the
content)\W*$|)( ?<content>.+)(? <footer>^\W*(fo oter
begins|fffff)\W *$.*|)
Try this:

string rex = @"\A((?<head>.* ?)^\W*(hhhhh|he ader ended|here comes the
content)\W*$)?( ?<content>.*?)( ^\W*(fffff|foot er begins)\W*$(?<f oot>.*))?\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******* *************@p hobos.telenet-ops.be>...
Hi,
[inline]

"Neri" <av****@lycos.c o.uk> wrote in message
news:87******** *************** ***@posting.goo gle.com...
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|her e comes the
content)\W*$|)( ?<content>.+)(? <footer>^\W*(fo oter
begins|fffff)\W *$.*|)


Try this:

string rex = @"\A((?<head>.* ?)^\W*(hhhhh|he ader ended|here comes the
content)\W*$)?( ?<content>.*?)( ^\W*(fffff|foot er begins)\W*$(?<f oot>.*))?\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.c o.uk> wrote in message
news:87******** *************** **@posting.goog le.com...
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*(f ffff|footer
begins)\W*\r\n( ?!.*?\r\n\W*(ff fff|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******* *************@p hobos.telenet-ops.be>...
Hi,
[inline]

"Neri" <av****@lycos.c o.uk> wrote in message
news:87******** *************** ***@posting.goo gle.com...
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|her e comes the
content)\W*$|)( ?<content>.+)(? <footer>^\W*(fo oter
begins|fffff)\W *$.*|)


Try this:

string rex = @"\A((?<head>.* ?)^\W*(hhhhh|he ader ended|here comes the
content)\W*$)?( ?<content>.*?)( ^\W*(fffff|foot er begins)\W*$(?<f oot>.*))?\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.c o.uk> wrote in message
news:87******** *************** **@posting.goog le.com...
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*(f ffff|footer
begins)\W*\r\n( ?!.*?\r\n\W*(ff fff|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******* *************@p hobos.telenet-ops.be>...
Hi,
[inline]

"Neri" <av****@lycos.c o.uk> wrote in message
news:87******** *************** ***@posting.goo gle.com...
> 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|her e comes the
> content)\W*$|)( ?<content>.+)(? <footer>^\W*(fo oter
> begins|fffff)\W *$.*|)

Try this:

string rex = @"\A((?<head>.* ?)^\W*(hhhhh|he ader ended|here comes the
content)\W*$)?( ?<content>.*?)( ^\W*(fffff|foot er begins)\W*$(?<f oot>.*))?\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
3693
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 pages. I have been programming in PHP for over 2 years now and have never encountered a problem like the one I am having now. To me this seems like it should be just about the simplest thing in the world, but I must admit I'm stumped BIG TIME!...
2
1723
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. For example, doubleswap(abcab)=bbaacbbaa. Furthermore, let doubleswap(L) be the language formed of strings doubleswap(x) for x an element of L. Prove that if L is regular, then so is doubleswap(L). Where L is a subset (or equal) to {a, b, c}^*.
2
1900
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 a in x by the substring bb and each b by the substring aa. For example, doubleswap(abcab)=bbaacbbaa. Furthermore, let doubleswap(L) be the language formed of strings doubleswap(x) for x an element of L. Prove that if L is regular, then so is...
4
2209
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 = " s^\?AAA\?01^BBB^g; #Comment "
5
477
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 characters, but at the 14th of this character it should fail. Thanks, Todd Meister
4
3144
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>. Please help Regards
1
3712
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 attach this script files and inq files. I cant understand this error. Please suggest me. You can talk with my yahoo id b_sahoo1@yahoo.com. Now i am online. Plz....Plz..Plz...
3
2413
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 them; these are: 1) Devise a coding system to send CAPITAL LETTERS using the ASCII code. Assume the lights will be ON when you start sending, and use OFF for ZERO and ON for ONE. 2. I want to use this code to send letters to someone else by...
1
1736
by: BHPexpert | last post by:
Regular Expression help needed -------------------------------------------------------------------------------- I want to extract all text that is contained inside the brackets after the word INDIRECT. There must be at least one pair of parentheses, they may be multiple pairs for exampple, this string '=IF($B$9="Off",0,INDIRECT(ADDRESS(1(8),COLUMNS($A$41:C41),,,$B$9 )),()aaaa' has four bracket pairings until the initial "(" is...
0
8703
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
8623
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9185
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8935
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5879
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3069
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2359
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.