RegexReplace(?) Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
parkgwansu

RegexReplace(?)

31 Mar 2016, 23:24

;-------exeample script-----------
text =
(
abcd10A00b1
accc20A20b2
ZZZZ30AXXXX40ACCCC
)
Result := RegexReplace(text,"U).*(\d{2}A).*", "$1 ")
;--------------------------------

Result = 10A 20A 30A 40A CCCC


But I Want..
Result = 10A 20A 30A 40A

How?
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: RegexReplace(?)

01 Apr 2016, 01:05

Try this:

Code: Select all

text =
(
abcd10A00b1
accc20A20b2
ZZZZ30AXXXX40ACCCC
)

Loop, Parse, text, `n, `r
{
    p := 1
    While p := RegexMatch(A_LoopField, "U).*(\d{2}A).*", $, p + StrLen($))
        Result .= $1 " "
}
MsgBox, % Result
I hope that helps.
tmplinshi
Posts: 1600
Joined: 01 Oct 2013, 14:57

Re: RegexReplace(?)  Topic is solved

01 Apr 2016, 02:12

Result := RegexReplace(text,"U).*(\d{2}A).*", "$1 ")
The last two characters .* of your regex has no effect. Because .* matches zero or more characters, and the ungreedy option "U" make it match as short as it can, so it chooses zero character.
So your regex can be shorten into "U).*(\d{2}A)", also same as ".*?(\d{2}A)".

Below code works, which added a second match .+$.

Code: Select all

result := RegExReplace(text, ".*?(\d{2}A)|.+$", "$1 ")
tmplinshi
Posts: 1600
Joined: 01 Oct 2013, 14:57

Re: RegexReplace(?)

01 Apr 2016, 02:33

Hi wolf_II

You can remove the Loop, Parse:

Code: Select all

text =
(
abcd10A00b1
accc20A20b2
ZZZZ30AXXXX40ACCCC
)

p := 1, m := ""
While p := RegexMatch(text, "\d{2}A", m, p + StrLen(m))
  Result .= m " "

MsgBox, % Result
parkgwansu

Re: RegexReplace(?)

01 Apr 2016, 02:58

wolf_II
tmplinshi

Thank you.
많은 도움이 되었습니다. 감사합니다.
User avatar
HinkerLoden
Posts: 93
Joined: 23 Mar 2016, 07:50
GitHub: HinkerLoden

Re: RegexReplace(?)

01 Apr 2016, 04:20

2 Questions

1.) Is there a usefull Tutorial for this RegEx
2.) Why should i use this instead of the given intern functions
User avatar
sinkfaze
Posts: 614
Joined: 01 Oct 2013, 08:01

Re: RegexReplace(?)

01 Apr 2016, 08:38

I can't guarantee it in all situations but it works for this one:

Code: Select all

text = 
( 
abcd10A00b1
accc20A20b2
ZZZZ30AXXXX40ACCCC
) 
MsgBox %	RegExReplace(RegExReplace(text,"`am)^\D*|[^\A]*$"),"A\K.*?(?=\d{2}A)"," ")
User avatar
sinkfaze
Posts: 614
Joined: 01 Oct 2013, 08:01

Re: RegexReplace(?)

01 Apr 2016, 09:01

HinkerLoden wrote:1.) Is there a useful Tutorial for this RegEx
I don't know if it was useful but I had written a series of posts as a beginner's tutorial for RegEx at the original old forums. When those forums were upgraded the post formatting was destroyed and the posts were pretty much useless. And even though these forums are similar to the original old forums, it would still take a considerable amount of time to clean out the pieces of deprecated post formatting and make them workable here, which I just don't have the time to do right now.
HinkerLoden wrote:2.) Why should i use this instead of the given intern functions
  1. RegExReplace() is an intern function,
  2. In this particular scenario literal matching would take a little more thought than pattern matching, but it is an interesting exercise to figure out:

Code: Select all

text = 
( 
abcd10A00b1
accc20A20b2
ZZZZ30AXXXX40ACCCC
)
While	p :=	InStr(text,"A",1,1,A_Index)
{
	n :=	SubStr(text,p-2,2)
	if n is number
		out .=	n "A "
}
MsgBox %	Trim(out)
need4speed
Posts: 132
Joined: 22 Apr 2016, 06:50

Re: RegexReplace(?)

22 Apr 2016, 07:01

HinkerLoden wrote: 1.) Is there a usefull Tutorial for this RegEx
try this.one

Return to “Ask For Help”

Who is online

Users browsing this forum: a1987zz, Aggronaught, Bing [Bot], mikeyww, Netocon and 41 guests