Add a period if required

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

Add a period if required

09 Apr 2024, 10:45

There should be a punctuation mark at the end of each line. If there is no period, question mark or
exclamation mark, a period should be added there:

Code: Select all

Example:
from
str := "First sentence.`nHere is something`nThird sentence!`nHere again`nLast sentence?"
becomes:
str := "First sentence.`nHere is something.`nThird sentence!`nHere again.`nLast sentence?"
What is the regular expression for doing this?
Thank you very much for your help.
User avatar
mikeyww
Posts: 27009
Joined: 09 Sep 2014, 18:38

Re: Add a period if required

09 Apr 2024, 11:22

Code: Select all

#Requires AutoHotkey v1.1.33.11
str := "First sentence.`nHere is something`nThird sentence!`nHere again`nLast sentence?"
MsgBox 64, Result, % str "`n`n" fix(str)

fix(str) {
 Static regex := "m`a)[^.?!]$"
 Return RegExReplace(str, regex, "$0.")
}
Last edited by mikeyww on 09 Apr 2024, 11:25, edited 1 time in total.
Rohwedder
Posts: 7659
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Add a period if required

09 Apr 2024, 11:23

Hallo,
try:

Code: Select all

str := "First sentence.`nHere is something`nThird sentence!`nHere again`nLast sentence?"
str := RegExReplace(str, "`am)([^[:punct:]]$)", "$1.")
MsgBox,% str

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: FanaticGuru and 203 guests