Regex help: Match everything after

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
leesho
Posts: 1
Joined: 22 Oct 2019, 09:53

Regex help: Match everything after

22 Oct 2019, 10:01

I'm stuck and could use a push in the right direction! Given the following text:
This is garbage: This is not
I would like to be able to copy the entire line, then paste everything after the colon:
This is not
I have the following script which almost works:

Code: Select all

^!V::              ;Ctrl-Alt-V 'Special Paste'

Haystack := Clipboard
Needle := "[^:]+$"

RegExMatch(Haystack, Needle, Match)
Clipboard := Match

Send, ^V
return
The problem is, I want to paste everything after the colon and the space that follows the colon. Right now this script pastes the space, too(I've placed an underline character to illustrate the problem):
_This is not
Any help? Thanks.
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Regex help: Match everything after

22 Oct 2019, 10:39

Code: Select all

RegExMatch("This is garbage: This is not", "[^:]*: \K.*", match)
MsgBox % match
match anything that isnt a colon, the colon, the space - discard them. match whatever is left.
guest3456
Posts: 3463
Joined: 09 Oct 2013, 10:31

Re: Regex help: Match everything after

22 Oct 2019, 10:45

i tend to just capture the pattern:

Code: Select all

Haystack := "This is garbage: This is not"
Needle := ":\s(.*)$"      ; find the colon ":" and a space "\s" and then capture everything after into subpattern1
RegExMatch(Haystack, Needle, match)
MsgBox % match1         ; match1

digidings
Posts: 24
Joined: 22 Jan 2018, 17:04

Re: Regex help: Match everything after

22 Oct 2019, 10:50

Another solution with look-behind assertion:

Code: Select all

^!V::              ;Ctrl-Alt-V 'Special Paste'

Haystack := Clipboard
Needle := "(?<=:)\s*(?<Keep>.+)$"

RegExMatch(Haystack, Needle, TextTo)
Clipboard := TextToKeep

Send, ^V
return

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Araphen, DaveT1, Descolada, Google [Bot] and 168 guests