Advertisement
Guest User

regexMatches

a guest
Jun 23rd, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- regexMatches :: String -> String -> [[String]]
  2. on regexMatches(strRegex, strHay)
  3.     set ca to current application
  4.     set NSNotFound to a reference to 9.22337203685477E+18 + 5807 -- for some reason ca's NSNotFound doesn't work
  5.     set oRgx to ca's NSRegularExpression's regularExpressionWithPattern:strRegex ¬
  6.         options:((ca's NSRegularExpressionAnchorsMatchLines as integer)) |error|:(missing value)
  7.     set oString to ca's NSString's stringWithString:strHay
  8.     set oMatches to oRgx's matchesInString:oString options:0 range:{location:0, |length|:oString's |length|()}
  9.    
  10.     set xs to {}
  11.     repeat with omatch in oMatches
  12.         set intMax to ((numberOfRanges of omatch) as integer) - 1
  13.        
  14.         set subs to {}
  15.         repeat with i from 0 to intMax
  16.             tell (omatch's rangeAtIndex:i)
  17.                 set intfrom to its location
  18.                 if intfrom ≠ NSNotFound then
  19.                     set end of subs to text (intfrom + 1) thru (intfrom + (its |length|)) of strHay
  20.                 else
  21.                     set end of subs to missing value
  22.                 end if
  23.             end tell
  24.         end repeat
  25.         set end of xs to subs
  26.        
  27.     end repeat
  28.     return xs
  29. end regexMatches
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement