Please help me with RegExMatch

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

Please help me with RegExMatch

27 Oct 2016, 04:45

I would like to get:
Term1 = AAA
Term2 = BBB
Term3 = ccc

But the result with my code is:
Term1 = AAA}, {BBB}, {ccc
Term2 =
Term3 =

What is wrong with my code? How to correct it? Thanks!

Code: Select all

  Str = My string {AAA}, {BBB}, {ccc}
  RegExMatch(Str, "i)\{(.+)\}",Term)
  Msgbox T1: %Term1%     T2: %Term3%         T3: %Term3%
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: Please help me with RegExMatch

27 Oct 2016, 05:01

Code: Select all

Str = My string {AAA}, {BBB}, {ccc}
Term := RegExMatchAll(Str, "i)\{(.+?)\}", 1)
MsgBox, % Term.1 "`n" Term.2 "`n" Term.3

RegExMatchAll(ByRef Haystack, NeedleRegEx, SubPat="") {
	arr := [], startPos := 1
	while ( pos := RegExMatch(Haystack, NeedleRegEx, match, startPos) ) {
		arr.push(match%SubPat%)
		startPos := pos + StrLen(match)
	}
	return arr.MaxIndex() ? arr : ""
}
ahcahc
Posts: 110
Joined: 25 Jul 2014, 23:55

Re: Please help me with RegExMatch

27 Oct 2016, 05:56

oldbrother wrote: What is wrong with my code? How to correct it? Thanks!
RegExMatch only returns the position of the first match. In order to search the rest of the string you need to keep on changing the starting position (can be accomplished by doing loops).

You can also do this with RegExReplace + StrSplit.

Code: Select all

Str = My string {AAA}, {BBB}, {ccc}
term := StrSplit(RegExReplace(str,"s).*?(?<=\{)(.+?)(?=\}).+?","$1`n"), "`n")
MsgBox % term.1 "`n" term.2 "`n" term.3

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 247 guests