A little Regex help

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
thegrove
Posts: 11
Joined: 16 Jun 2020, 09:43

A little Regex help

02 Dec 2021, 11:54

I have some strings of this format which I want to split into parts as follows

Code: Select all

Split | some | text 2002 | but it does not work
into

Code: Select all

some
text 2002
i.e. discarding the "|" and "Split" and "but it does not work" parts.

I'm using this here:

Code: Select all

RegExMatch(MyStr, "\s\|\s(.*)\s\|\s", MyOut)
MsgBox, % MyOut
This is giving returning " | some | text 2002 | " rather than the first part I want. I realise I may need to do this in two passes (that's the second part of the question) to get the two parts but this is not behaving as I expect.

Thanks.
User avatar
mikeyww
Posts: 26883
Joined: 09 Sep 2014, 18:38

Re: A little Regex help

02 Dec 2021, 12:22

Code: Select all

str  = Split | some | text 2002 | but it does not work
arr := StrSplit(str, " | ")
SendInput % arr.2 "`n" arr.3
thegrove
Posts: 11
Joined: 16 Jun 2020, 09:43

Re: A little Regex help

02 Dec 2021, 12:26

Thank you, that's a neater solution.

How would I have done it with RegExMatch?
Rohwedder
Posts: 7627
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: A little Regex help

02 Dec 2021, 12:43

Hallo,
StrSplit is better!

Code: Select all

str  = Split | some | text 2002 | but it does not work
RegExMatch(str, "([^|]+)\|([^|]+)\|([^|]+)\|([^|]+)", arr)
SendInput % arr2 "`n" arr3
User avatar
mikeyww
Posts: 26883
Joined: 09 Sep 2014, 18:38

Re: A little Regex help

02 Dec 2021, 13:08

Here is another, though success depends on how variations of this format occur.

Code: Select all

str  = Split | some | text 2002 | but it does not work
RegExMatch(str, "\|(.+)\|", m)
SendInput % StrReplace(Trim(m1), " | ", "`n")

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: ishida20, jameswrightesq, Lem2001 and 415 guests