-
Notifications
You must be signed in to change notification settings - Fork 71
/
PCREAHK-REPLACE.AHK
71 lines (66 loc) · 2.32 KB
/
PCREAHK-REPLACE.AHK
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
; Example wrapper for PCRE regular expressions: replacing
; For details about PCRE see http://www.pcre.org or http://mushclient.com/pcre/
; Parameters:
; string: where to replace
; offset: start offset in string; 0 means first byte
; pattern: what to search, can be any PCRE compatible regex (see PCRE docs)
; options: 1 does a case-insensitive match; 2 ignores newlines; 3 does both
; replace: the replacement, can be any string. There are a few special
; cases: ${ refers to everything in string before the found item(s); $} to
; everything after the found item(s); $0 is the whole found string; $1 to $9
; refer to captured substrings (again, the details are in the PCRE docs).
; Return value: negative in case of an error (codes in the PCRE docs), or 0
; if nothing was replaced, or 1 if something was replaced.
RE_Replace(ByRef string,offset,pattern,options,replace)
{
; IMPORTANT: reserve enough space for the complete replacement string!
VarSetCapacity(tempres,1024)
tempres:=string
; call the DLL function
i:=DllCall("pcreahk.dll\pcre_replace","str",tempres,"int",offset,"str",pattern,"int",options,"str",replace)
If (i>0)
; at least one replacement was done, copy result back
string:=tempres
VarSetCapacity(tempres,0)
Return i
}
hModule:=DllCall("LoadLibrary","str","pcreahk.dll","UInt")
p:="simple"
s:="a simple test, really simple"
o:=0
r:="${very simple$}"
e:=RE_Replace(s,o,p,0,r)
if (e>0)
MsgBox result of RE_Replace: %s%
Else
MsgBox Error: %e%
; jump over the first "simple"
o:=InStr(s,p)+StrLen(p)-1
e:=RE_Replace(s,o,p,0,r)
if (e>0)
MsgBox result of RE_Replace: %s%
Else
MsgBox Error: %e%
; Three captured substrings: 2 ts with at least 1 digit in between,
; then anything, and again 2 ts with at least 1 digit in between.
; If p looks like Greek to you, check the PCRE docs.
p:="(t[0-9]+t)(.*)(t[0-9]+t)"
s:="begin_t88888888t_middle_t123t_end"
r:="$}[[$3]]--[[$2]]--[[$1]]${"
e:=RE_Replace(s,0,p,0,r)
if (e>0)
MsgBox result of RE_Replace: %s%
Else
MsgBox Error: %e%
; Three captured substrings: a word, a colon surrounded by whitespace
; and another word
p:="(\w+)(\s+:\s+)(\w+)"
s:="left : right"
r:="original was: ""$0""; replacement is: ""$3$2$1"""
e:=RE_Replace(s,0,p,0,r)
if (e>0)
MsgBox result of RE_Replace: %s%
Else
MsgBox Error: %e%
DllCall("FreeLibrary","UInt",hModule)
Return