473,738 Members | 7,110 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 3226
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
3700
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
1727
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
1903
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
2212
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
3147
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
3717
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
2415
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
1738
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
8969
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
8788
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
9335
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8210
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6751
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4570
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4825
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3279
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
2745
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.