Board index » delphi » Search/replace over multiple lines

Search/replace over multiple lines

How can I read an entire file into memory, loop through it to check
for some text (stretching over multiple lines), and then replace those
lines with something else?

For example:

"This is
a text
containing
five
lines"

I need to search for:

"a text
containing"

and replace it with:

"a short paragraph
consisting of"

In other words, I need to do a regular search/replace, over multiple
lines.

Please respond both to group and by mail.

Thanks,
Jarle Aasland

 

Re:Search/replace over multiple lines


Quote
Jarle Aasland wrote:

> How can I read an entire file into memory, loop through it to check
> for some text (stretching over multiple lines), and then replace those
> lines with something else?

> For example:

> "This is
> a text
> containing
> five
> lines"

> I need to search for:

> "a text
> containing"

> and replace it with:

> "a short paragraph
> consisting of"

> In other words, I need to do a regular search/replace, over multiple
> lines.

> Please respond both to group and by mail.

> Thanks,
> Jarle Aasland

I would think that your best bet would be to stream the text into
memory, perhaps one byte at a time, and run a byte comparison function
against the desired search string.  Ignore carriage returns and line
feeds and you're groovin'.

Re:Search/replace over multiple lines


If you are using D3 there is a Find Dialog and a Find/Replace Dialog Box on the
tool bar that should do what you want.

For greater detail and an example, see the March 1998 Delphi Developer's
Journal.

HTH

:-)
Bill Burt, Jr.
BBur...@AOL.COM

Re:Search/replace over multiple lines


Quote
Jarle Aasland wrote:

> How can I read an entire file into memory, loop through it to check
> for some text (stretching over multiple lines), and then replace those
> lines with something else?

> For example:

> "This is
> a text
> containing
> five
> lines"

> I need to search for:

> "a text
> containing"

> and replace it with:

> "a short paragraph
> consisting of"

> In other words, I need to do a regular search/replace, over multiple
> lines.

Jarle,

    if you want to do a regular expression search, you maybe want to
have a look at the unit STRINGL which contains a class TGrep. TGrep does
all the regular expression search stuff similar to the unix grep
command. STRINGL is available from one of the following websites:

    http://www.lohninger.com/stringl.html
or
    http://qspr03.tuwien.ac.at/lo/stringl.html

Best Regards,

   Hans

-----------------------------------
  Hans Lohninger
  Vienna University of Technology
  Institute of General Chemistry
  Getreidemarkt 9/152
  A-1060 Vienna, Austria
  hlohn...@email.tuwien.ac.at
  http://qspr03.tuwien.ac.at/lo/
  http://www.lohninger.com/
  phone: ++43-1-58801-5048
  fax: ++43-1-581-1915
-----------------------------------

Other Threads