regex help Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
awcrt9316
Posts: 61
Joined: 03 Mar 2020, 20:06

regex help

16 May 2020, 13:46

say if I want to get the second and third word in a string
this will acomplish that

Code: Select all

line := "first second third fourth fifth"
    RegExMatch(Line, "^(?:.*?\K\b\S+\b){2}", word1)    ; gets second word
    RegExMatch(Line, "^(?:.*?\K\b\S+\b){3}", word2)    ; gets third word
    MsgBox, % word :=  word1 " "word2
is there any way to shorten this regex stuff so it is just one line of code that extracts both words instead of writing it like this?
User avatar
flyingDman
Posts: 870
Joined: 29 Sep 2013, 19:01

Re: regex help

16 May 2020, 14:16

w/o regex:

Code: Select all

line := "first second third fourth fifth"
msgbox % strsplit(line," ").2 " " strsplit(line," ").3
awcrt9316
Posts: 61
Joined: 03 Mar 2020, 20:06

Re: regex help

16 May 2020, 14:18

the only problem with this for me is there is a random amount of spaces between each word, so that wouldn't work. thats why i was using regex
teadrinker
Posts: 1901
Joined: 29 Mar 2015, 09:41
Contact:

Re: regex help

16 May 2020, 15:07

Code: Select all

line := "first second third fourth fifth"
RegExMatch(line, "(\S+)\s+(\S+)", m)
MsgBox, % m1 "`n" m2
User avatar
boiler
Posts: 6597
Joined: 21 Dec 2014, 02:44

Re: regex help

16 May 2020, 15:12

Slight mod to the above to get the requested second and third words.

Code: Select all

line := "first second third fourth fifth"
RegExMatch(line, "\S+\s+(\S+)\s+(\S+)", m)
MsgBox, % m1 "`n" m2
User avatar
flyingDman
Posts: 870
Joined: 29 Sep 2013, 19:01

Re: regex help

16 May 2020, 15:43

Or,

Code: Select all

line := "first       second      third          fourth fifth"
msgbox % regexreplace(line, "\S+\s+(\S+)\s+(\S+).*", "$1 $2")
sofista
Posts: 36
Joined: 24 Feb 2020, 13:59

Re: regex help

16 May 2020, 18:05

awcrt9316 wrote:
16 May 2020, 14:18
the only problem with this for me is there is a random amount of spaces between each word, so that wouldn't work. thats why i was using regex
Try this instead:

Code: Select all

line := "first       second      third          fourth fifth"
while InStr(line, "  ")
	line := StrReplace(line, "  ", " ")
MsgBox % strsplit(line," ").2 " " strsplit(line," ").3
awcrt9316
Posts: 61
Joined: 03 Mar 2020, 20:06

Re: regex help  Topic is solved

16 May 2020, 21:31

sofista wrote:
16 May 2020, 18:05
awcrt9316 wrote:
16 May 2020, 14:18
the only problem with this for me is there is a random amount of spaces between each word, so that wouldn't work. thats why i was using regex
Try this instead:

Code: Select all

line := "first       second      third          fourth fifth"
while InStr(line, "  ")
	line := StrReplace(line, "  ", " ")
MsgBox % strsplit(line," ").2 " " strsplit(line," ").3
almost works, but i needed to implement autotrim

Code: Select all

line := "    	asdfasdf        éeconé     third          fourth fifth  "
line = %line% ; forces line to be autotrimmed (no whitespace at beginning or end)
while InStr(line, "  ")
	line := StrReplace(line, "  ", " ")
msgbox, % line
MsgBox % strsplit(line," ").2

Return to “Ask For Help”

Who is online

Users browsing this forum: Google [Bot], hugin, mikeyww, Previz, TAC109 and 63 guests