help with RegExReplace

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
need4speed
Posts: 143
Joined: 22 Apr 2016, 06:50

help with RegExReplace

18 May 2016, 09:07

i'd like to extract a text pattern from the clipboard and write it to a text file line by line.

the Textfile Content in the end should be like this:
A1234567890
B123456789012

the clipboard Content could be something like this:
bla bla text A 123 123 123 11 11 bla bla return, line feed
more text B 123 456768 9012 bla bla and so on

so the pattern I'm looking for is starting with a A followed by 10 Digits e.g. A1234567890 or coould start with B followed by 12 Digits eg. B123456789012

the problem are the space character and maybe return and line feed
in my approach I first use this code to get rid of all space characters, this seems to work well:

Code: Select all

Clipboard := RegExReplace(Clipboard, "[\s+]")
by using a regex tester i found this needle to work correctly after getting rid of the space chars

Code: Select all

((a|A)\d{10})|((b|B)\d{12})
in the end after extracting the pattern I use this code to write to the file.

Code: Select all

FileAppend, %clipboard%, test.txt
I found somewhere the code bellow to but I dunno how to add my needle and in order to get the wanted result.

thanks 4 your help

Code: Select all

#x:: ;[Win]+[X]

;Empty the Clipboard.
    Clipboard =
;Copy the select text to the Clipboard.
    SendInput, ^c
;Wait for the Clipboard to fill.
    ClipWait

;Perform the RegEx find and replace operation,
;where "ABC" is the whole-word we want to replace.
    haystack := Clipboard
    needle := "\b" . "ABC" . "\b"
    replacement := "XYZ"
    result := RegExReplace(haystack, needle, replacement)

;Empty the Clipboard
    Clipboard =
;Copy the result to the Clipboard.
    Clipboard := result
;Wait for the Clipboard to fill.
    ClipWait

;-- Optional: --
;Send (paste) the contents of the new Clipboard.
    SendInput, %Clipboard%

;Done!
    return
User avatar
AlphaBravo
Posts: 586
Joined: 29 Sep 2013, 22:59

Re: help with RegExReplace

18 May 2016, 11:44

Code: Select all

h=
(
the clipboard Content could be something like this:
bla bla text A 123 45
6 789 01 11 bla bla return, line feed
more text B 123 45678 9012 bla bla and so on
)
res := ""
while pos := RegExMatch(H, "\bA(\s*\d){10}|\bB(\s*\d){12}", m, A_Index=1?1:pos+StrLen(m))
	res .= (res?"`n":"") RegExReplace(m, "\s+")
MsgBox % res
; FileAppend, % res, FileName
need4speed
Posts: 143
Joined: 22 Apr 2016, 06:50

Re: help with RegExReplace

19 May 2016, 01:47

thanks.

unfortunatelly it does not work when there is no space between the text pattern.
bellow code shows only two instead of three:

Code: Select all

h=
(
bla bla A1234567890A1234567890 B123456789012
bla bla
)
nor does it work with lower case letters

Code: Select all

h=
(
bla bla a1234567890 A1234567890 B123456789012
bla bla
)
User avatar
AlphaBravo
Posts: 586
Joined: 29 Sep 2013, 22:59

Re: help with RegExReplace

19 May 2016, 08:46

need4speed wrote:unfortunatelly it does not work when there is no space between the text pattern.
hence better details are always recommended!

Code: Select all

h=
(
bla bla text A 123 45
6 789 01 11 bla bla return, line feed
more text B 223 45678 9012 bla bla and so on
bla bla A3234567890A4234567890 B523456789012
bla bla a6234567890 A7234567890 B823456789012
bla bla
)
res := ""
while pos := RegExMatch(H, "i)A(\s*\d){10}|B(\s*\d){12}", m, A_Index=1?1:pos+StrLen(m))
	res .= (res?"`n":"") RegExReplace(m, "\s+")
MsgBox % res
need4speed
Posts: 143
Joined: 22 Apr 2016, 06:50

Re: help with RegExReplace

19 May 2016, 10:03

thanks AlphaBravo, :bravo: works perfect!
need4speed
Posts: 143
Joined: 22 Apr 2016, 06:50

Re: help with RegExReplace

24 May 2016, 08:44

any idea how to count and Display how often the search pattern was found?
User avatar
AlphaBravo
Posts: 586
Joined: 29 Sep 2013, 22:59

Re: help with RegExReplace

24 May 2016, 10:38

Code: Select all

h=
(
bla bla text A 123 45
6 789 01 11 bla bla return, line feed
more text B 223 45678 9012 bla bla and so on
bla bla A3234567890A4234567890 B523456789012
bla bla a6234567890 A7234567890 B823456789012
bla bla
)
res := ""
while pos := RegExMatch(H, "i)A(\s*\d){10}|B(\s*\d){12}", m, A_Index=1?1:pos+StrLen(m))
	res .= (res?"`n":"") RegExReplace(m, "\s+") , Frequency := A_Index
MsgBox % res "`n`nPattern was found " Frequency " times"
need4speed
Posts: 143
Joined: 22 Apr 2016, 06:50

Re: help with RegExReplace

25 May 2016, 03:25

thanks again! :dance:

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: ht55cd3, Joey5, lechat, mamo691, sebalotek and 383 guests