Page 1 of 1

Adding a full stop to a heading using a regular expression

Posted: 27 Mar 2024, 06:42
by Billykid
textfile before:

Code: Select all

Bla bla bla bla.  Bla bla bla bla.  Bla bla bla bla.  Bla bla bla bla.  Bla bla bla bla.
Bla bla bla bla.  Bla bla bla bla.  Bla bla bla bla.  Bla bla bla bla.  Bla bla bla bla.
Bla bla bla bla.  Bla bla bla bla.  Bla bla bla bla.  Bla bla bla bla.  Bla bla bla bla.

This is a my first heading   ; adding a full stop to headline here

Bla bla bla bla.  Bla bla bla bla.  Bla bla bla bla.  Bla bla bla bla.  Bla bla bla bla.
Bla bla bla bla.  Bla bla bla bla.  Bla bla bla bla.  Bla bla bla bla.  Bla bla bla bla.
Bla bla bla bla.  Bla bla bla bla.  Bla bla bla bla.  Bla bla bla bla.  Bla bla bla bla.

Another heading that extends over more than one line Another heading that 
extends over more than one line   ; adding a full stop to headline here

Bla bla bla bla.  Bla bla bla bla.  Bla bla bla bla.  Bla bla bla bla.  Bla bla bla bla.
Bla bla bla bla.  Bla bla bla bla.  Bla bla bla bla.  Bla bla bla bla.  Bla bla bla bla.
Bla bla bla bla.  Bla bla bla bla.  Bla bla bla bla.  Bla bla bla bla.  Bla bla bla bla.


textfile after:

Code: Select all

Bla bla bla bla.  Bla bla bla bla.  Bla bla bla bla.  Bla bla bla bla.  Bla bla bla bla.
Bla bla bla bla.  Bla bla bla bla.  Bla bla bla bla.  Bla bla bla bla.  Bla bla bla bla.
Bla bla bla bla.  Bla bla bla bla.  Bla bla bla bla.  Bla bla bla bla.  Bla bla bla bla.

This is a my first heading.   ; heading now with fullstop

Bla bla bla bla.  Bla bla bla bla.  Bla bla bla bla.  Bla bla bla bla.  Bla bla bla bla.
Bla bla bla bla.  Bla bla bla bla.  Bla bla bla bla.  Bla bla bla bla.  Bla bla bla bla.
Bla bla bla bla.  Bla bla bla bla.  Bla bla bla bla.  Bla bla bla bla.  Bla bla bla bla.

Another heading that extends over more than one line Another heading that 
extends over more than one line.   ; heading now with full stop

Bla bla bla bla.  Bla bla bla bla.  Bla bla bla bla.  Bla bla bla bla.  Bla bla bla bla.
Bla bla bla bla.  Bla bla bla bla.  Bla bla bla bla.  Bla bla bla bla.  Bla bla bla bla.
Bla bla bla bla.  Bla bla bla bla.  Bla bla bla bla.  Bla bla bla bla.  Bla bla bla bla.

I have a text file that consists of normal text and headings between the individual text sections. The headings between the
text sections are characterised by the fact that they do not have a full stop, they are separated by at least one blank line before and after the next text section.
The text headings can also extend over several lines. A full stop should simply be added to these headings as a punctuation mark.
How can this be achieved with a regular expression?

Re: Adding a full stop to a heading using a regular expression

Posted: 27 Mar 2024, 06:51
by mikeyww
What does "extend over several lines" mean?

Re: Adding a full stop to a heading using a regular expression

Posted: 27 Mar 2024, 07:21
by Billykid
Thanks for your question.
By this I mean that a heading can extend over several lines. The main characteristic of a heading is that it does not contain a closing punctuation mark such as a full stop.

Example:

This is also a headline that extends
over several lines but it doesn't have
a full stop at the end

Re: Adding a full stop to a heading using a regular expression

Posted: 27 Mar 2024, 08:19
by mikeyww

Code: Select all

#Requires AutoHotkey v1.1.33.11
filePath := "test.txt"
f := FileOpen(filePath, "rw")
txt := f.Read(), f.Pos(0)
f.Write(addStop(txt)) ; CAUTION: File will be overwritten
f.Close()
Run % filePath

addStop(str) {
 Static regex := "([^\s.])(\h*?\R{2})"
 Return RegExReplace(str, regex, "$1.$2")
}