RegEx Help Line Should End with Period, if no period at the end of line.

Get help with using AutoHotkey and its commands and hotkeys
ahklearner
Posts: 309
Joined: 23 Jan 2015, 01:49

RegEx Help Line Should End with Period, if no period at the end of line.

01 Mar 2016, 02:13

Hi Community :wave:

Need help to insert period at the end of each line if period does not exist.

Code: Select all

MsgBox % RegExReplace(Clipboard, "m).$",".")
Sample:
Say no to 1 hour meetings
Do the Steve Jobs walk.
Visualize your success

Expectation:
Say no to 1 hour meetings.
Do the Steve Jobs walk.
Visualize your success.

Can somebody guide me how to check for a particular char OR word does not exist at end of the line.

I know I can do this thing with read file, A_LoopReadLine, and check for StringRight, and if it does not containt period then just add after A_LoopReadLine, but I want to do it with regex believe it will be much shorter and helpful in the future.

Thanks in advance for your help.
User avatar
jmeneses
Posts: 492
Joined: 28 Oct 2014, 11:09
Location: Catalan Republic

Re: RegEx Help Line Should End with Period, if no period at the end of line.

01 Mar 2016, 03:10

Hi ahklearner try this

Code: Select all

string := "
(
Say no to 1 hour meetings
Do the Steve Jobs walk.
Visualize your success  
)"
;Any non char period follow by any CR LF and replace group by period and linefeed
msgbox % RegExReplace(string,"([^.])\R|$","$1.`n")                  
exitapp
Donec Perficiam
ahklearner
Posts: 309
Joined: 23 Jan 2015, 01:49

Re: RegEx Help Line Should End with Period, if no period at the end of line.

01 Mar 2016, 03:17

Hi jmeneses,

Thanks for the help, but it is giving period to empty lines also and adding one more line in between.

Please try it with some empty lines in between.
User avatar
jmeneses
Posts: 492
Joined: 28 Oct 2014, 11:09
Location: Catalan Republic

Re: RegEx Help Line Should End with Period, if no period at the end of line.

01 Mar 2016, 03:22

Code: Select all

msgbox % RegExReplace(string,"([^.]\w)\R|$","$1.`n")       
Donec Perficiam
ahklearner
Posts: 309
Joined: 23 Jan 2015, 01:49

Re: RegEx Help Line Should End with Period, if no period at the end of line.

01 Mar 2016, 03:46

Hi jmeneses,

The above code is wonderful, though I found in some cases when the line is ending with a number, the period is not suffixing.

Thanks again for the help with RegEx.
User avatar
sinkfaze
Posts: 614
Joined: 01 Oct 2013, 08:01

Re: RegEx Help Line Should End with Period, if no period at the end of line.

01 Mar 2016, 08:34

Code: Select all

string=
(
Say no to 1 hour meetings
Do the Steve Jobs walk.
Visualize your success  
)

MsgBox %	RegExReplace(string,"`am)[^\.]$","$0.")
ahcahc
Posts: 110
Joined: 25 Jul 2014, 23:55

Re: RegEx Help Line Should End with Period, if no period at the end of line.

01 Mar 2016, 10:42

Code: Select all

string := "
(
Say no to 1 hour meetings
Do the Steve Jobs walk.
Visualize your success      
alot of space(s) after this                       
a number 123


there are blank lines above and below



some text

)"
MsgBox % RegExReplace(string,"`am)[ \t]+$|(?<=\w)$",".")
User avatar
AlphaBravo
Posts: 476
Joined: 29 Sep 2013, 22:59

Re: RegEx Help Line Should End with Period, if no period at the end of line.

01 Mar 2016, 10:44

This also handles empty lines and lines ending with white spaces

Code: Select all

string=
(
Say no to 1 hour meetings
Following line is empty

Do the Steve Jobs walk.
Visualize your success   
)
string.="`nWhite spaces at end of line     "

MsgBox % RegExReplace(string,"`am)[^\r\n\. ]\K\h*$",".")
ahklearner
Posts: 309
Joined: 23 Jan 2015, 01:49

Re: RegEx Help Line Should End with Period, if no period at the end of line.

01 Mar 2016, 10:45

Thanks a lot for the wonderful help and support !
could you guys please explain a lil bit, how actually is this working...

i understand basics like `am and $ sign for line ending but "^\." & "$0." unable get anything for this, what logic lies behind this.

Thanks in advance for your cooperation.
User avatar
AlphaBravo
Posts: 476
Joined: 29 Sep 2013, 22:59

Re: RegEx Help Line Should End with Period, if no period at the end of line.

01 Mar 2016, 11:00

here you go ;)

Code: Select all

`am)				# `a:recognizes any type of newline, m: Multiline
[^\r\n\. ]		# Character not in [\r\n\. ] Character Class		- find anything that is not [\r\n. ]
\K				# <Reset start of match>							- discard it
\h				# <horizontal white space character>				- find horizontal white space
*				# (zero or more)(greedy)							- if any
$				# End of string/line								- at the end of string

replace match with "."
ahklearner
Posts: 309
Joined: 23 Jan 2015, 01:49

Re: RegEx Help Line Should End with Period, if no period at the end of line.

01 Mar 2016, 12:23

Thank you for all suggestions, every help, replies, and comments.

May our creator bless you with best of both worlds :)
User avatar
sinkfaze
Posts: 614
Joined: 01 Oct 2013, 08:01

Re: RegEx Help Line Should End with Period, if no period at the end of line.

01 Mar 2016, 12:40

As an addendum to AlphaBravo's code, if you have trailing horizontal whitespace and would like to preserve it:

Code: Select all

string=
(
Say no to 1 hour meetings

Following line is empty
 
Do the Steve Jobs walk.
Visualize your success   
)

string.=	"`nWhite spaces at end of line     "
 
MsgBox %	RegExReplace(string,"`ams)[^\R\.]\K\h*$",".$0")

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], JerkyJesse and 34 guests