Issue extracting values using Regex Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Youri11
Posts: 23
Joined: 23 Apr 2021, 08:57

Issue extracting values using Regex

23 Apr 2021, 09:14

If I do the following :

Code: Select all

i:="Hello12 3"
RegExMatch(i,"([^ ]* )(\d)$",o)
a:=o1
b:=o2
msgbox,% a "--" b
RegExMatch(a,"(.+)(\d+)$",p)
c:=p1
d:=p2
msgbox,% c "--" d
I get empty values for c and d.

While this works perfectly:

Code: Select all

RegExMatch("Hello12","(.+)(\d+)$",p)
c:=p1
d:=p2
msgbox,% c "--" d
I don't understand what is wrong with my first attempt. Please could you help?

Thank you.
Last edited by Youri11 on 23 Apr 2021, 10:12, edited 1 time in total.
User avatar
Smile_
Posts: 858
Joined: 03 May 2020, 00:51

Re: Issue extracting values using Regex

23 Apr 2021, 09:24

There is space with a variable "Hello12( )" in the first attempt which is not present in the second.
Youri11
Posts: 23
Joined: 23 Apr 2021, 08:57

Re: Issue extracting values using Regex

23 Apr 2021, 09:44

Oh! Yes. Thank you very much Smile_ for spotting this obvious mistake I wasn't able too see.

I realize I should write my regex differently then. Let me look into it.
User avatar
boiler
Posts: 16973
Joined: 21 Dec 2014, 02:44

Re: Issue extracting values using Regex  Topic is solved

23 Apr 2021, 09:45

You can just remove the $ to address the issue Smile_ pointed out.

Is your last script really working perfectly? Is your desired result really Hello1--2 instead of Hello--12? That wouldn't seem to be the intention since you have \d+ for the second part. If you really want the latter, this would do it:

Code: Select all

i:="Hello12 3"
RegExMatch(i,"([^ ]* )(\d)$",o)
a:=o1
b:=o2
msgbox,% a "--" b
RegExMatch(a,"(\D+)(\d+)",p)
c:=p1
d:=p2
msgbox,% c "--" d
Youri11
Posts: 23
Joined: 23 Apr 2021, 08:57

Re: Issue extracting values using Regex

23 Apr 2021, 11:38

Thank you also boiler for offering ways to fix my issue. This is appreciated.
Removing the $ indeed fix my mistake. You are totally right about my intention as my desired output is effectively Hello--12

Code: Select all

i:="Hello12 3"
RegExMatch(i,"([^ ]* )(\d)$",o)
a:=o1
b:=o2
msgbox,% a "--" b
RegExMatch(a,"(.*?)(\d+)",p)
c:=p1
d:=p2
msgbox,% c "--" d
Thank you both.
All the best to you.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], ShatterCoder and 12 guests