Simple search and replace doesn't function

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
ineuw
Posts: 172
Joined: 11 Sep 2014, 14:12

Simple search and replace doesn't function

08 Jun 2015, 01:32

I am trying to replace two spaces with one space in a block of text and it doesn't work. Can someone please look at this code and correct the mistake?

Code: Select all

;Win+z  Test
#z::
Text:=""
Clipboard:=""
SendInput, ^a
SendInput, ^c
Text:=Clipboard
Clipboard:=""
;two spaces
Text:=StrReplace(Text, "m)   "," ")
Clipboard:=Text
SendInput ^a
SendInput ^v
Sendinput, ^{Home}
Return
Win 10 Professional 64bit 21H2 16Gb Ram AHK current as of 2021-12-26 .
Guest

Re: Simple search and replace doesn't function

08 Jun 2015, 01:51

You are using RegExReplace options with StrReplace, that will never work as it now looks for m bracket and two spaces. Replace :-) StrReplace with RegExReplace and it should work. StrReplace can not handle wildcards or regular expressions.
User avatar
ineuw
Posts: 172
Joined: 11 Sep 2014, 14:12

Re: Simple search and replace doesn't function

08 Jun 2015, 03:00

Thanks for your reply but I already tried the following:

Code: Select all

Text: = RegexReplace(Text,"m)  "," ") 
Text: = StrReplace(Text,"  "," ") 
Text = StrReplace(Text,"  "," ") 
Text: = StrReplace(%Text%,"  "," ") 
Text = StrReplace(Text,"  ",A_Space)
and other as well, like

Code: Select all

StringReplace, OutputVar, InputVar, SearchText [, ReplaceText, ReplaceAll?]
and no luck with any one of them.
Win 10 Professional 64bit 21H2 16Gb Ram AHK current as of 2021-12-26 .
Guest

Re: Simple search and replace doesn't function

08 Jun 2015, 03:21

(see note below, all your code examples are invalid, there should be no space between : and = )

Most likely what you are seeing is not what is there, you might think it is a space but it might be a tab character (\t) or a non breaking space (alt-0160).

Copy the text into a decent text editor to "see" what is there - most editors will show or can show the value of the character at the caret. It might not be a space. You could try \s or \X for specific unicode chars. Try your find/replace in a text editor which supports RE, if it works there you can get it to work in AutoHotkey as well I'm sure.

Code: Select all

Text := RegExReplace(Text,"m)\s\s"," ") ; read  the regex quickref below
Another option would be to replace all tabs, non-breaking spaces and other funny chars with spaces before you do your double to single replacement(s).

See also http://ahkscript.org/docs/misc/RegEx-QuickRef.htm

btw all your code examples are invalid and will never work

Code: Select all

Text: = RegExReplace(Text,"m)  "," ")  ; invalid, space between : =
Text: = StrReplace(Text,"  "," ")      ; invalid, space between : =
Text = StrReplace(Text,"  "," ")       ; invalid because you lack : in := 
Text: = StrReplace(%Text%,"  "," ")    ; invalid because you use %% and no space between : =
Text = StrReplace(Text,"  ",A_Space)   ; invalid because you lack : in :=
User avatar
ineuw
Posts: 172
Joined: 11 Sep 2014, 14:12

Re: Simple search and replace doesn't function

08 Jun 2015, 15:35

Thanks again for the reply and the Regex code to clean the text. It was my stupidity for inserting the spaces between the equal sign here on the forum for clearer readability (for myself) and that caused the confusion. I learned a valuable lesson and the actual code has no spaces. (See current code pasted below). I am using TextPad 7.51 to write code and clean the text in question.
TextPad's regular expression syntax is based on that used by the programming language Perl. It is synonymous with ECMAScript and JavaScript regular expressions.
I think it's the same as Autohotkey.

Unfortunately the code doesn't work and I can't get past this obstacle to continue with a series of simple search and replace operations to clean the text, which is a block of OCR plain text from a Wikisource webpage: https://en.wikisource.org/w/index.php?t ... ction=edit

The scan creates a variety of typographic errors which are easily corrected by a macro/script. Currently, I am using TextPad's built in macro system. It's fast and 100% accurate but unfortunately it's not editable and the only way to add to the code is to rewrite it, an option I don't relish. Another reason is that while my default browser has always been Firefox, sometimes I use a collection of four browsers to test CSS modifications for Wikisource. Using AHK would free me from using FireFox / TextPad relationship (via FF addons) and facilitate cleaning the text directly in the web page.

Code: Select all

;Win+z Test
#z::
Text= ""
Clipboard= ""
SendInput, ^a
SendInput, ^c
Text:= Clipboard
Clipboard= ""
;clean Regex chars
Text:= RegExReplace(Text,"m)\s\s"," ")
;two spaces
Text:= StrReplace(Text, "  "," ")
Clipboard:= Text
SendInput ^a
SendInput ^v
Sendinput, ^{Home}
Return
Win 10 Professional 64bit 21H2 16Gb Ram AHK current as of 2021-12-26 .
gilliduck
Posts: 265
Joined: 06 Oct 2014, 15:01

Re: Simple search and replace doesn't function

08 Jun 2015, 16:30

This works for me:

Code: Select all

#Z::
Clipboard := ""
Send, ^a^c
Double_Space := A_Space . A_Space
Single_Space := A_Space
Clipboard := StrReplace(Clipboard, Double_Space, Single_Space)
Send, ^v^{Home}
Return
User avatar
ineuw
Posts: 172
Joined: 11 Sep 2014, 14:12

Re: Simple search and replace doesn't function

08 Jun 2015, 20:05

Many thanks gilliduck. It works great. Assigning the values to variables and operating on the variables humbles me.
Win 10 Professional 64bit 21H2 16Gb Ram AHK current as of 2021-12-26 .
gilliduck
Posts: 265
Joined: 06 Oct 2014, 15:01

Re: Simple search and replace doesn't function

08 Jun 2015, 20:18

I'm as guilty as anyone (more than others) of over complicating my code, but just try to keep it to a bare minimum of functionality and it tends to work. One issue I had was SendInput, it was actually too fast, that's why I dropped it down to Send. It was sending Ctrl A and then Ctrl C faster than the computer could highlight everything.
Guest

Re: Simple search and replace doesn't function

09 Jun 2015, 03:18

It is probably the Send not the variables that make the difference, in future you could /should check with MsgBox % text or MsgBox % clipboard to see if the variable or clipboard you are operating on actually have the data you expect it to have - that might have been the problem because for me there is no change between using on " " directly and doublespace:=A_Space . A_Space.

Anyway, glad to hear its working now.
User avatar
ineuw
Posts: 172
Joined: 11 Sep 2014, 14:12

Re: Simple search and replace doesn't function

09 Jun 2015, 14:48

In two instances I had to use Clipwait, 1 because it kept missing a simple text replacement. Thanks to you both, I am almost up and running. I resolved all Regexp searches and everything is working fine except the following:

The mdash is the only search and replace that I couldn't resolve {u+x2014} is the Unicode code and {Asc 0151} is the ASCII/ANSI code. If one uses Send, {Asc 0151} or Send, {u+x2014} they both return the — character.

Code: Select all

;Win+z Test
#z::
Clipboard := ""
Send, ^a^c

;strip space preceeding mdash 
in_put := " —"
out_put := "—"
Clipboard := StrReplace(Clipboard, in_put, out_put)

;strip space following mdash
in_put := "— "
out_put := "—"
Clipboard := StrReplace(Clipboard, in_put, out_put)

;strip double curly braces surrounding mdash from previous script action to avoid {{{{—}}}}
in_put := "{{—}}"
out_put := "—"
Clipboard := RegexReplace(Clipboard, in_put, out_put)

;surround mdash with double curly braces
in_put := "—"
out_put := "{{—}}"
Clipboard := StrReplace(Clipboard, in_put, out_put)

Send, ^v^{Home}
Return
Win 10 Professional 64bit 21H2 16Gb Ram AHK current as of 2021-12-26 .
Guest

Re: Simple search and replace doesn't function

09 Jun 2015, 15:19

Just to be sure: you are using AHK unicode and you are saving your scripts as utf-8? http://ahkscript.org/docs/Scripts.htm#cp :?:
User avatar
ineuw
Posts: 172
Joined: 11 Sep 2014, 14:12

Re: Simple search and replace doesn't function

09 Jun 2015, 17:03

Yes, it's definitely UTF-8, I have the following code pages: ANSI, DOS, UTF-8, Unicode or Binary. The file type selection can be PC, UNIX, Mac, Netscape or Unknown, I have PC selected.
Win 10 Professional 64bit 21H2 16Gb Ram AHK current as of 2021-12-26 .
User avatar
berban
Posts: 97
Joined: 14 Apr 2014, 03:20

Re: Simple search and replace doesn't function

09 Jun 2015, 18:47

ineuw, just a minor suggestion regarding performance, each Clipboard operation takes a long time compared to other AutoHotkey commands so you might be better off storing the text into a temp variable, e.g.

Code: Select all

temp := Clipboard
temp := StringReplace(temp, in_put, out_put)
temp := StringReplace(temp, in_put, out_put)
Clipboard := temp
Regarding your issues I'd just echo what guest said in making sure your file is saved as UTF-8, and also make sure those are actually mdashes? Cause they look the same as a regular dash haha.
lexikos
Posts: 9592
Joined: 30 Sep 2013, 04:07
Contact:

Re: Simple search and replace doesn't function

09 Jun 2015, 21:35

It must be not only be UTF-8, but also have the UTF-8 byte order mark. Some editors call it "UTF-8" and some call it "UTF-8 with BOM". You can use the following to check how AutoHotkey interprets the file:

Code: Select all

FileEncoding cp0  ; Reset the default just in case.
MsgBox % "File encoding: " FileOpen(A_ScriptFullPath, "r").Encoding
	. "`nSystem default ANSI codepage: CP" DllCall("GetACP")
Alternatively, Chr(0x2014) returns if you're using a Unicode version of AutoHotkey.

If you copy just that character to the clipboard, you can check its character code like so:

Code: Select all

MsgBox % Asc(Clipboard)  ; Returns the first ANSI byte or UTF-16 code unit of Clipboard.
berban wrote:temp variable
I'd recommend using #NoEnv in all scripts, but this is one case where it's especially important. If you don't use #NoEnv and temp happens to be empty, it will return the value of the TEMP environment variable instead.
User avatar
ineuw
Posts: 172
Joined: 11 Sep 2014, 14:12

Re: Simple search and replace doesn't function

10 Jun 2015, 00:44

Thanks berban for the suggestion. Initially the output container was a variable, and I was surprised that the script operates directly on the clipboard's contents. Unless your concern is about lost data, (that was my concern), the original text is still preserved. As for speed, the amount of text is being formatted at any given time is between 450 to 750 words max and the script performs the changes so fast that I had to insert "clipwait, 0" between each operation.

lexikos: The script and the text in TextPad, it's now writing in Unicode and UTF-8 BOM. Windows 7 OS file encoding is ANSI 1252

What I really need is the correct syntax in defining the string in such a complex search.

(If you hold down the Alt key, and tap 0151 on the numeric keypad, you'll get the (ANSI) mdash character. )

These are my initial AHK script settings

Code: Select all

#Include D:\AHKFunctionsLibrary\SearchAndReplace.ahk
#InstallKeybdHook
#NoEnv ;Recommended for performance and compatibility with future AutoHotkey releases.
#Warn  ;Enable warnings to assist with detecting common errors.

AutoTrim, Off

;Alt+m  em dash —
!m::
;Send, {Asc 0151}
Send, {u+2014}
Return
Win 10 Professional 64bit 21H2 16Gb Ram AHK current as of 2021-12-26 .
lexikos
Posts: 9592
Joined: 30 Sep 2013, 04:07
Contact:

Re: Simple search and replace doesn't function

10 Jun 2015, 01:55

I may be missing something, but I don't see how it's a "complex" search. If the code you posted earlier with "—" is not working, either the string doesn't contain what you thought it does, or your file is not encoded correctly.
User avatar
ineuw
Posts: 172
Joined: 11 Sep 2014, 14:12

Re: Simple search and replace doesn't function

10 Jun 2015, 02:01

I will try again and will post the results later this morning. Must get some sleep. :-) Thanks lexikos.
Win 10 Professional 64bit 21H2 16Gb Ram AHK current as of 2021-12-26 .
Guest

Re: Simple search and replace doesn't function

10 Jun 2015, 02:01

Here is a quick test, if this works and your other script doesn't, it probably means that it isn't an mdash but a figure dash or horizontal bar (‒ – — ―) http://en.wikipedia.org/wiki/Dash in that case you may wish to perform three find/replace actions to catch them all or use a RegExReplace to do it all at once.

Code: Select all

text:="some test with — an mdash"
in_put := "—"
out_put := "{{—}}"
MsgBox % StrReplace(Text, in_put, out_put) ; will show some test with {{—}} an mdash
User avatar
ineuw
Posts: 172
Joined: 11 Sep 2014, 14:12

Re: Simple search and replace doesn't function

10 Jun 2015, 02:17

My gratitude to all for your kind help. It works perfectly. Now I can get some sleep. :-)
Win 10 Professional 64bit 21H2 16Gb Ram AHK current as of 2021-12-26 .

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot] and 318 guests