Regexp Help Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
hasantr
Posts: 933
Joined: 05 Apr 2016, 14:18
Location: İstanbul

Regexp Help

18 May 2020, 16:39

Can it be done in one line with Regexp? I want to take the model.

String:

Code: Select all

List of devices attached
C4F12571E35588E        device product:GT-P7300 model:GT_P7300 device:p5 transport_id:4
f2554a3d0205           device product:daisy_sprout model:Mi_A2_Lite device:daisy transport_id:6
192.168.1.35:5556      device product:daisy_sprout model:Mi_A2_Lite device:daisy transport_id:7

Code: Select all

Loop,Parse,inDevicesList,`n
list .= RegExMatch(A_LoopField,"(model:).*( device:)", Out) "`n"


Image

Desired Results:
1: GT_P7300
2: Mi_A2_Lite
3: Mi_A2_Lite
User avatar
boiler
Posts: 17192
Joined: 21 Dec 2014, 02:44

Re: Regexp Help

18 May 2020, 16:56

This does it without the 1:, 2:, 3: prefixes:

Code: Select all

Clip =
(
List of devices attached
C4F12571E35588E        device product:GT-P7300 model:GT_P7300 device:p5 transport_id:4
f2554a3d0205           device product:daisy_sprout model:Mi_A2_Lite device:daisy transport_id:6
192.168.1.35:5556      device product:daisy_sprout model:Mi_A2_Lite device:daisy transport_id:7
)
Out := RegExReplace(Clip, ".*?model:(.*?) device:\w+ transport_id:.", "$1`n")
MsgBox, % Out
hasantr
Posts: 933
Joined: 05 Apr 2016, 14:18
Location: İstanbul

Re: Regexp Help

18 May 2020, 17:00

boiler wrote:
18 May 2020, 16:56
This does it without the 1:, 2:, 3: prefixes:

Code: Select all

Clip =
(
List of devices attached
C4F12571E35588E        device product:GT-P7300 model:GT_P7300 device:p5 transport_id:4
f2554a3d0205           device product:daisy_sprout model:Mi_A2_Lite device:daisy transport_id:6
192.168.1.35:5556      device product:daisy_sprout model:Mi_A2_Lite device:daisy transport_id:7
)
Out := RegExReplace(Clip, ".*?model:(.*?) device:\w+ transport_id:.", "$1`n")
MsgBox, % Out
Thanks. This is beautiful.
Can you explain a little how it works?

Edit:

Can RegexReplace return Array as in StrSplit?

Out[1] ; GT_P7300
Out[2] ; Mi_A2_Lite
Out[3] ; Mi_A2_Lite
User avatar
boiler
Posts: 17192
Joined: 21 Dec 2014, 02:44

Re: Regexp Help

18 May 2020, 17:13

We're taking advantage of the fact that RegExReplace will replace all instances (unlike RegExMatch, which only finds one instance at a time), so we can use it to find all instances in one command. Then the need is to replace everything in the string but the piece of interest, so we need to match everything then reference the piece of interest in the replacement string.

The parenthesis identify a subpattern which we can include in the replacement string by identifying it as $1 (for the first one, subsequent ones would have been $2, $3, etc., but we only have one). We need to match everything so it's replaced only by our identified subpattern, thus the need to match everything leading up to it with .*?model: (the .*? matches everything leading up to it in a non-greedy manner) as well as everything after it, which includes all the "device" and "transport" stuff including the "wildcards" to make it match the changing parts. So when you replace everything with just the subpattern ($1) and a linefeed character (`n), you're left with just the stuff you want on separate lines.
User avatar
boiler
Posts: 17192
Joined: 21 Dec 2014, 02:44

Re: Regexp Help  Topic is solved

18 May 2020, 17:19

hasantr wrote:
18 May 2020, 17:00
Can RegexReplace return Array as in StrSplit?

Out[1] ; GT_P7300
Out[2] ; Mi_A2_Lite
Out[3] ; Mi_A2_Lite
I only know how to do it with a subsequent StrSplit:

Code: Select all

Clip =
(
List of devices attached
C4F12571E35588E        device product:GT-P7300 model:GT_P7300 device:p5 transport_id:4
f2554a3d0205           device product:daisy_sprout model:Mi_A2_Lite device:daisy transport_id:6
192.168.1.35:5556      device product:daisy_sprout model:Mi_A2_Lite device:daisy transport_id:7
)
Models := RegExReplace(Clip, ".*?model:(.*?) device:\w+ transport_id:.", "$1`n")
Models := StrSplit(RTrim(Models, "`n"), "`n")

For Each, Model in Models
	Out .= "Out[" Each "]: " Model "`n"
MsgBox, % Out
hasantr
Posts: 933
Joined: 05 Apr 2016, 14:18
Location: İstanbul

Re: Regexp Help

18 May 2020, 17:25

Thank you @Kazan . Thanks to you, we learn a lot.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], mikeyww, PAT06, RaptorX, science2002 and 81 guests