Regular Expression help, please.

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
oldbrother
Posts: 273
Joined: 23 Oct 2013, 05:08

Regular Expression help, please.

18 Apr 2020, 11:07

Can one line RegExReplace code do this job? How?

Add 10 to all numbers leading with "x" in the text:

Text: aa x1.2 bb x2.3 cc x45 ddd x1.2 ee x8
to: aa x11.2 bb x12.3 cc x55 ddd x11.2 ee x18

Thanks!
User avatar
AlphaBravo
Posts: 586
Joined: 29 Sep 2013, 22:59

Re: Regular Expression help, please.

18 Apr 2020, 12:38

Code: Select all

Text = aa x1.2 bb x2.3 cc x45 ddd x1.2 ee x8
while pos := RegExMatch(Text, "x(\d+)", m, A_Index=1?1:pos+StrLen(m))
	Text := RegExReplace(Text, "x\K\d+", m1+10,, 1, pos)

MsgBox % text
User avatar
oldbrother
Posts: 273
Joined: 23 Oct 2013, 05:08

Re: Regular Expression help, please.

18 Apr 2020, 13:13

Thank you AlphaBravo! It works perfectly!

What is the function of "K" in "x\K\d+"? I cannot find it from the help file.
User avatar
boiler
Posts: 16957
Joined: 21 Dec 2014, 02:44

Re: Regular Expression help, please.

18 Apr 2020, 13:22

https://www.autohotkey.com/docs/misc/RegEx-QuickRef.htm wrote:The escape sequence \K is similar to a look-behind assertion because it causes any previously-matched characters to be omitted from the final matched string. For example, foo\Kbar matches "foobar" but reports that it has matched "bar".
User avatar
oldbrother
Posts: 273
Joined: 23 Oct 2013, 05:08

Re: Regular Expression help, please.

18 Apr 2020, 13:33

Thank you all! Have a great weekend! :thumbup:

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 190 guests