Adding a full stop to a heading using a regular expression

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Billykid
Posts: 101
Joined: 16 Sep 2019, 08:59

Adding a full stop to a heading using a regular expression

Post by Billykid » 27 Mar 2024, 06:42

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?
User avatar
mikeyww
Posts: 28871
Joined: 09 Sep 2014, 18:38

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

Post by mikeyww » 27 Mar 2024, 06:51

What does "extend over several lines" mean?
Billykid
Posts: 101
Joined: 16 Sep 2019, 08:59

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

Post by Billykid » 27 Mar 2024, 07:21

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
User avatar
mikeyww
Posts: 28871
Joined: 09 Sep 2014, 18:38

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

Post by mikeyww » 27 Mar 2024, 08:19

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")
}
Post Reply

Return to “Ask for Help (v1)”