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 4 3092
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
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
"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
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
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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....
|
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...
|
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 = " ...
|
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...
|
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>....
|
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...
|
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...
|
by: BHPexpert |
last post by:
Regular Expression help needed
--------------------------------------------------------------------------------
I want to extract all text that is contained inside the brackets after the word...
|
by: linyimin |
last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
|
by: erikbower65 |
last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA:
1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
|
by: kcodez |
last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: DJRhino1175 |
last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this -
If...
|
by: DJRhino |
last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer)
If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _
310030356 Or 310030359 Or 310030362 Or...
|
by: lllomh |
last post by:
How does React native implement an English player?
|
by: Mushico |
last post by:
How to calculate date of retirement from date of birth
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
| |