Regular expression to apply in a TXT file

Get help with using AutoHotkey and its commands and hotkeys
Guill
Posts: 132
Joined: 09 Jun 2016, 22:00

Regular expression to apply in a TXT file

22 Aug 2019, 11:01

Hi Guys

I have text files (*.txt) that I've copied from PDF files, and I need to delete wrong Carriage Returns

I suppose I need a Regular Expression, but I can't write it by myself. It should do something like that:

"get to the end of the line (Send ^{End}); if there is a point (Full stop), go down to the next line, otherwise, "Send %A_Space%" and "Send {Del}"

(The "A_Space" is in case the line ends without a space, and {Del} so that it suppresses the carriage Return, and, therefore, the bottom line continue next to the line I am working on.)

Thanks in advance


(Sorry by my English. It is not my mother language)
User avatar
Sir Teddy the First
Posts: 94
Joined: 05 Aug 2019, 12:31
Contact:

Re: Regular expression to apply in a TXT file

22 Aug 2019, 15:56

Hi,
I just came up with this. I hope it does what you wanted.
The Text is just an example, just copy that function and call it whenever you need it.

Code: Select all

Text := "This Text is    `nsplit into two lines!`r`nThere are mutliple sentences too."

Text := PolishText(Text)
MsgBox % Text

return


PolishText(Text)
{
	Text := StrReplace(Text, "`r`n", "")
	Text := RegExReplace(Text, " *?[`r`n]+", " ")

	FoundPos := 1
	Loop
	{
		FoundPos := RegExMatch(Text, "[[:punct:]]", PunctChar, (FoundPos + 1))
		Text := StrReplace(Text, PunctChar, PunctChar . "`n",, Limit := 1)
	}Until !FoundPos
	
	Return Text
}
:eh: :think:
Guill
Posts: 132
Joined: 09 Jun 2016, 22:00

Re: Regular expression to apply in a TXT file

22 Aug 2019, 16:18

Sir Teddy the First wrote:
22 Aug 2019, 15:56
Hi,
I just came up with this. I hope it does what you wanted.
The Text is just an example, just copy that function and call it whenever you need it.

Code: Select all

Text := "This Text is    `nsplit into two lines!`r`nThere are mutliple sentences too."

Text := PolishText(Text)
MsgBox % Text

return


PolishText(Text)
{
	Text := StrReplace(Text, "`r`n", "")
	Text := RegExReplace(Text, " *?[`r`n]+", " ")

	FoundPos := 1
	Loop
	{
		FoundPos := RegExMatch(Text, "[[:punct:]]", PunctChar, (FoundPos + 1))
		Text := StrReplace(Text, PunctChar, PunctChar . "`n",, Limit := 1)
	}Until !FoundPos
	
	Return Text
}


Thank you very much Sir Teddy the First

How could I adapt this script to use Clipboard in this sense?

"Initial text: What is on the Clipboard

Corrected text: That It be assign to the new Clipboard so that I can paste it into the TXT file."
ahk7
Posts: 336
Joined: 06 Nov 2013, 16:35

Re: Regular expression to apply in a TXT file

22 Aug 2019, 16:26

Code: Select all

; regex https://stackoverflow.com/questions/57600792/regex-to-unwrap-paragraphs-remove-returns-and-new-lines-at-end-of-lines-that-ha
; Clipboard:=RegExReplace(clipboard,"(?<!\r\n)\r\n(?!\r\n)"," ")

text=
(join`r`n
AutoHotkey doesn't do anything on its own; it needs a script to tell it what to do.

A script is simply a plain text file with the .ahk filename
extension containing instructions for the program, like a
configuration file, but much more powerful.

A script can do as little as performing a single action
and then exiting, but most scripts define a number of
hotkeys, with each hotkey followed by one or more actions
to take when the hotkey is pressed.

Tip: If your browser supports it, you can download
any code block (such as the one above) as a script
file by clicking the button which appears in the
top-right of the code block when you hover your mouse over it.

)

FileDelete, Test_TestFile.txt
FileAppend, % RegExReplace(text,"(?<!\r\n)\r\n(?!\r\n)"," "), Test_TestFile.txt
:?:

Return to “Ask For Help”

Who is online

Users browsing this forum: AHKStudent, alexsu, Bing [Bot], famko, Muzzy and 42 guests