Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

grep() - global regular expression match


  • Please log in to reply
23 replies to this topic
widow
  • Guests
  • Last active:
  • Joined: --
any update?

BGM
  • Members
  • 199 posts
  • Last active: Jun 05 2015 06:26 PM
  • Joined: 12 Jan 2011

@polyethene - thanks for a wonderful script!  I have a post here on the Stack Exchange with a nice application of it!


(AutoHotkey 1.1.15.00 on Windows 7 Pro x64)


just me
  • Members
  • 1496 posts
  • Last active: Nov 03 2015 04:32 PM
  • Joined: 28 May 2011

That's true, I usually escape my commas prior to calling grep. Until real arrays/objects are supported AutoHotkey will always have a problem here.

 

Well, the time has come!

 

#NoEnv
SetBatchLines, -1

Haystack := "
(Join`s
How can capture all the numbers in a given string?
It shouldn't matter whether they are floating point numbers, integers, positive or negative.
It should capture 50 or 100.25 or 12345678 or -78.999 each as a numbered capture.
)"

Matches := RegExMatchGlobal(Haystack, "Ox)[-+]?\d+(\.\d+)?")
Result := ""
For I, Match In Matches
   Result .= I . ": Pos = " . Match.Pos(0) . ", Len = " . Match.Len(0) . ", Val = " Match.Value(0) . "`n"
MsgBox, 0, % Matches.MaxIndex() . " Matches", % Result
ExitApp

; ======================================================================================================================
; Function: RegExMatchGlobal()
; Returns an array containing the overall match objects for NeedleRegEx in Haystack (see RegExMatch()).
; The option O (match object) will be added internally if needed.
; ======================================================================================================================
RegExMatchGlobal(ByRef Haystack, NeedleRegEx) {
   Static Options := "U)^[imsxACDJOPSUX`a`n`r]+\)"
   NeedleRegEx := (RegExMatch(NeedleRegEx, Options, Opt) ? (InStr(Opt, "O", 1) ? "" : "O") : "O)") . NeedleRegEx
   Match := {Len: {0: 0}}, Matches := [], FoundPos := 1
   While (FoundPos := RegExMatch(Haystack, NeedleRegEx, Match, FoundPos + Match.Len[0]))
      Matches[A_Index] := Match
   Return Matches
}

 

 


Prefer ahkscript.org for the time being.


BGM
  • Members
  • 199 posts
  • Last active: Jun 05 2015 06:26 PM
  • Joined: 12 Jan 2011

Is it just me, or is that a rather nicely written function?  cool.png Thank you for sharing!


(AutoHotkey 1.1.15.00 on Windows 7 Pro x64)


BGM
  • Members
  • 199 posts
  • Last active: Jun 05 2015 06:26 PM
  • Joined: 12 Jan 2011

Just Me, how do I get the value of, say, the second number? Or, say, the string that contains every match?  I can't seem to get that.

 

matches := RegExMatchGlobal(Haystack, "Ox)[-+]?\d+(\.\d+)?")

 

;msgbox, % Matches.MaxIndex()       ;total number of matches

msgbox, % matches.value(2)            ;value of second match

msgbox, % matches.value(0)            ;get string with all matches


(AutoHotkey 1.1.15.00 on Windows 7 Pro x64)


just me
  • Members
  • 1496 posts
  • Last active: Nov 03 2015 04:32 PM
  • Joined: 28 May 2011

Matches is an array of objects:

MsgBox, % Matches.MaxIndex()       ; total number of match objects
MsgBox, % Matches[1].Value(2)      ; value of second subpattern of the first match object
MsgBox, % Matches[1].Value(0)      ; value of the overall match of the first match object
MsgBox, % Matches[1].Count()       ; the overall number of subpatterns of the first match object 

 


Prefer ahkscript.org for the time being.


BGM
  • Members
  • 199 posts
  • Last active: Jun 05 2015 06:26 PM
  • Joined: 12 Jan 2011

Ah, I get it now - I didn't realize I had to specify the index of two different arrays.  Thank you very much.


(AutoHotkey 1.1.15.00 on Windows 7 Pro x64)


thenomad
  • Members
  • 2 posts
  • Last active: Sep 26 2015 07:52 PM
  • Joined: 23 Jul 2013

I am having hard time understanding how this should work. I only dabble in AHK, not more than a beginner for sure, but I have 20+ years unix sysadmin background and the use of grep on any shell is what I am familiar with. I am sure this one has a different syntax.

 

This is what I am trying to do:

 

I am doing a screen scraping from a webapp

I am copying everything into clipboard

clipboard contents are something like 

 

fluff....fluff..... more fluff...

fluff....fluff..... more fluff...

fluff....fluff..... more fluff...

fluff....fluff..... more fluff...xxxx;yyyy

fluff....fluff..... more fluff...

fluff....fluff..... more fluff...

...

fluff....fluff..... more fluff...

 

I need to find the line with the semicolon and parse the strings on the left and right of that semicolon

 

The first thing I need is to find the line with semicolon in it and I thought I could use this grep function.

The meat of the script sits at the bottom of my ahk script

I am calling it right after I finished my screens scrape as :

 

grep ( myclipboard, ";", v)

msgbox %v%

 

and my msgbox comes up empty

I am sure I am doing something wrong, but what, I can not figure out. 

 

Thanks



space
  • Members
  • 520 posts
  • Last active:
  • Joined: 12 Aug 2014
You don't need grep in this case - You can use a simple RegExMatch and process the found line from there (variable m here)
clipboard=
(join`r`n
fluff....fluff..... more fluff...
fluff....fluff..... more fluff...
fluff....fluff..... more fluff...
fluff....fluff..... more fluff...xxxx;yyyy
fluff....fluff..... more fluff...
fluff....fluff..... more fluff...
fluff....fluff..... more fluff...
)

RegExMatch(clipboard,"m)^(.*);(.*)$",m)
MsgBox % m